Documento 1 Practica Vectores

Importar el dataset UKgas y almacenar la columna del dataset en un vector llamado consumo.de.gas.

data("UKgas")
consumo.de.gas <- as.vector(UKgas)

Comprobamos si el vector ‘consumo.de.gas’ es del tipo deseado:

class(consumo.de.gas)
## [1] "numeric"
  1. Acceder a los 10 primeros elementos del vector ‘consumo.de.gas’:
vals1 <- consumo.de.gas[c(1:10)]
vals1
##  [1] 160.1 129.7  84.8 120.1 160.1 124.9  84.8 116.9 169.7 140.9
  1. Acceder a los elementos impares del vector ‘consumo.de.gas’:
vals2 <- consumo.de.gas[c(TRUE, FALSE)]
vals2
##  [1]  160.1   84.8  160.1   84.8  169.7   89.7  187.3   92.9  176.1   89.7
## [11]  185.7   99.3  200.1  102.5  204.9  112.1  227.3  115.3  244.9  118.5
## [21]  244.9  188.9  301.0  136.1  317.0  152.1  371.4  158.5  449.9  179.3
## [31]  491.5  177.7  593.9  176.1  584.3  187.3  669.2  216.1  827.7  209.7
## [41]  840.5  217.7  848.5  209.7  925.3  214.5  917.3  224.1  989.4  233.7
## [51] 1087.0  281.8 1163.9  347.4
  1. Acceder a las posiciones 1,4,7,10,… del vector ‘consumo.de.gas’:
vals3 <- consumo.de.gas[c(TRUE, FALSE, FALSE)]
vals3
##  [1] 160.1 120.1  84.8 140.9 187.3 120.1  89.7 155.3 200.1 136.1 112.1 195.3
## [13] 244.9 153.7 188.9 196.9 317.0 336.2 158.5 286.6 491.5 409.8 176.1 395.4
## [25] 669.2 509.1 209.7 414.6 848.5 701.2 214.5 515.5 989.4 730.0 281.8 613.1
  1. Acceder al vector consumo.de.gas en orden inverso
vals4 <- consumo.de.gas[c(length(consumo.de.gas):1)]
vals4
##   [1]  782.8  347.4  613.1 1163.9  787.6  281.8  534.7 1087.0  730.0  233.7
##  [11]  477.1  989.4  694.8  224.1  515.5  917.3  683.6  214.5  443.4  925.3
##  [21]  701.2  209.7  437.0  848.5  670.8  217.7  414.6  840.5  542.7  209.7
##  [31]  467.5  827.7  509.1  216.1  421.0  669.2  485.1  187.3  395.4  584.3
##  [41]  483.5  176.1  329.8  593.9  409.8  177.7  321.8  491.5  403.4  179.3
##  [51]  286.6  449.9  355.4  158.5  240.1  371.4  336.2  152.1  230.5  317.0
##  [61]  267.3  136.1  196.9  301.0  142.5  188.9  216.1  244.9  153.7  118.5
##  [71]  214.5  244.9  142.5  115.3  195.3  227.3  140.9  112.1  176.1  204.9
##  [81]  136.1  102.5  161.7  200.1  131.3   99.3  155.3  185.7  123.3   89.7
##  [91]  147.3  176.1  120.1   92.9  144.1  187.3  123.3   89.7  140.9  169.7
## [101]  116.9   84.8  124.9  160.1  120.1   84.8  129.7  160.1
  1. Acceder a los 50 primeros elementos del vector consumo.de.gas excepto la posición 1, 3 y 5.
vals5 <- consumo.de.gas[1:50][-c(1,3,5)]
vals5
##  [1] 129.7 120.1 124.9  84.8 116.9 169.7 140.9  89.7 123.3 187.3 144.1  92.9
## [13] 120.1 176.1 147.3  89.7 123.3 185.7 155.3  99.3 131.3 200.1 161.7 102.5
## [25] 136.1 204.9 176.1 112.1 140.9 227.3 195.3 115.3 142.5 244.9 214.5 118.5
## [37] 153.7 244.9 216.1 188.9 142.5 301.0 196.9 136.1 267.3 317.0 230.5
  1. Sumarle 1 a los elementos del vector ‘consumo.de.gas’ (reciclaje):
vals6 <- 1 + consumo.de.gas
vals6
##   [1]  161.1  130.7   85.8  121.1  161.1  125.9   85.8  117.9  170.7  141.9
##  [11]   90.7  124.3  188.3  145.1   93.9  121.1  177.1  148.3   90.7  124.3
##  [21]  186.7  156.3  100.3  132.3  201.1  162.7  103.5  137.1  205.9  177.1
##  [31]  113.1  141.9  228.3  196.3  116.3  143.5  245.9  215.5  119.5  154.7
##  [41]  245.9  217.1  189.9  143.5  302.0  197.9  137.1  268.3  318.0  231.5
##  [51]  153.1  337.2  372.4  241.1  159.5  356.4  450.9  287.6  180.3  404.4
##  [61]  492.5  322.8  178.7  410.8  594.9  330.8  177.1  484.5  585.3  396.4
##  [71]  188.3  486.1  670.2  422.0  217.1  510.1  828.7  468.5  210.7  543.7
##  [81]  841.5  415.6  218.7  671.8  849.5  438.0  210.7  702.2  926.3  444.4
##  [91]  215.5  684.6  918.3  516.5  225.1  695.8  990.4  478.1  234.7  731.0
## [101] 1088.0  535.7  282.8  788.6 1164.9  614.1  348.4  783.8
  1. Sumar los 10 primeros elementos del vector consumo.de.gas con el evector v2 =(1,2,3):
v2 <- c(1,2,3)
vals7 <- c(consumo.de.gas[1:9] + v2, consumo.de.gas[10] + v2[1])
vals7
##  [1] 161.1 131.7  87.8 121.1 162.1 127.9  85.8 118.9 172.7 141.9

Podemos hacerlo también de la siguiente manera:

v2 <- c(1,2,3)
aux1 <- consumo.de.gas[1:12]
v7.2 <- aux1 + v2
v7.2[1:10]
##  [1] 161.1 131.7  87.8 121.1 162.1 127.9  85.8 118.9 172.7 141.9