Chapter 3 Manipulando datos
## # A tibble: 6 × 2
## Country `GDP (current, M€)`
## <chr> <dbl>
## 1 Belgium 620272.
## 2 Bulgaria 104767.
## 3 Czechia 320786.
## 4 Denmark 392401.
## 5 Germany 4328970
## 6 Estonia 39848.
## # A tibble: 6 × 17
## age job marital education default balance housing loan contact day month duration campaign pdays previous poutcome
## <dbl> <chr> <chr> <chr> <chr> <dbl> <chr> <chr> <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <chr>
## 1 30 unemploy… married primary no 1787 no no cellul… 19 oct 79 1 -1 0 unknown
## 2 33 services married secondary no 4789 yes yes cellul… 11 may 220 1 339 4 failure
## 3 35 manageme… single tertiary no 1350 yes no cellul… 16 apr 185 1 330 1 failure
## 4 30 manageme… married tertiary no 1476 yes yes unknown 3 jun 199 4 -1 0 unknown
## 5 59 blue-col… married secondary no 0 yes no unknown 5 may 226 1 -1 0 unknown
## 6 35 manageme… single tertiary no 747 no no cellul… 23 feb 141 2 176 3 failure
## # ℹ 1 more variable: y <chr>
Vemos los datos anteriores contienen diversas observaciones (filas) y variables (columnas). Pero los nombres de las columnas están en inglés y además son demasiado complejos para manejarlo en un programa estadístico. Vamos a simplificar los nombres:
names(datos_econ) <- c("pais", "pib")
names(datos_mkt) <- c("edad", "trab", "civil" ,"educ", "def", "balance", "casa", "prestam", "contrato", "dia", "mes", "durac", "camp", "pdias", "previo", "pres", "y")
# La función names() extrae los nombres de 'datos'. Usando <- estoy introduciendo los nombres los encabezados de 'datos'.
head(datos_mkt)## # A tibble: 6 × 17
## edad trab civil educ def balance casa prestam contrato dia mes durac camp pdias previo pres y
## <dbl> <chr> <chr> <chr> <chr> <dbl> <chr> <chr> <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
## 1 30 unemployed married primary no 1787 no no cellular 19 oct 79 1 -1 0 unknown no
## 2 33 services married secondary no 4789 yes yes cellular 11 may 220 1 339 4 failure no
## 3 35 management single tertiary no 1350 yes no cellular 16 apr 185 1 330 1 failure no
## 4 30 management married tertiary no 1476 yes yes unknown 3 jun 199 4 -1 0 unknown no
## 5 59 blue-collar married secondary no 0 yes no unknown 5 may 226 1 -1 0 unknown no
## 6 35 management single tertiary no 747 no no cellular 23 feb 141 2 176 3 failure no
## # A tibble: 6 × 2
## pais pib
## <chr> <dbl>
## 1 Belgium 620272.
## 2 Bulgaria 104767.
## 3 Czechia 320786.
## 4 Denmark 392401.
## 5 Germany 4328970
## 6 Estonia 39848.
# También podemos escribir 'datos_mkt' o 'datos_econ' directamente en la consola para que me muestre el contenido del objeto.¿Pero qué es la función c() que estoy usando? ¡Fácil! c() simplemente nos ayuda a crear otro objeto: un vector de datos, algo así como una fila individual de datos. Voy a crear uno que se llame info que contenga información sobre mi:
# Voy a crear un vector de dos elementos donde el primero sea mi nombre y el segundo mi profesión.
info <- c("Daniel", "Profesor")
# Si luego escribimos 'info' en la consola se mostrará el contenido:
info## [1] "Daniel" "Profesor"
De hecho, cada columna de datos puede extraerse como un vector, es muy sencillo. Simplemente hay que escribir datos$columna donde columna deberá tener el nombre de la variable que queremos extraer:
## [1] 620271.8 104767.2 320786.5 392400.7 4328970.0 39847.7 562794.2 236736.0 1594330.0 2919899.9 85905.2 2199619.4
## [13] 34770.2 40359.4 78996.2 86180.3 206040.1 23136.7 1122459.0 494087.6 848490.9 289784.3 353633.1 67418.1
## [25] 130207.5 275993.0 558672.8 30696.5 446866.1 896448.7 27483.1 7644.5 15672.0 25000.7 83257.9 1253386.7
## [1] 30 33 35 30 59 35 36 39 41 43 39 43 36 20 31 40 56 37 25 31 38 42 44 44 26 41 55 67 56 53 68 31 59 32 49 42 78 32 33 23
## [41] 38 36 52 32 32 34 55 26 32 61 45 37 38 34 53 48 57 33 36 54 41 63 48 48 56 51 31 29 41 32 37 42 31 41 56 54 41 40 50 40
## [81] 27 60 28 52 37 51 41 35 38 34 39 34 27 32 57 41 41 30 36 31 36 38 49 57 34 32 37 33 56 27 21 58 32 33 25 25 30 22 39 46
## [121] 31 45 32 36 51 34 24 45 32 77 41 37 43 48 27 33 29 34 52 60 32 32 56 31 42 35 56 48 46 37 41 36 45 55 39 35 55 55 27 34
## [161] 34 25 41 43 28 31 78 45 48 32 42 37 40 32 42 43 44 37 37 37 28 32 34 40 34 34 34 37 36 56 49 33 30 36 23 57 27 38 42 75
## [201] 34 48 35 39 29 45 42 29 38 56 36 25 38 57 31 42 45 30 49 49 53 37 49 41 54 32 33 33 36 35 52 40 44 45 30 48 33 57 57 51
## [241] 34 50 44 27 50 36 42 32 42 43 31 32 55 58 46 55 34 28 42 29 54 32 39 53 58 40 29 33 38 56 40 24 46 35 35 34 35 38 50 51
## [281] 59 32 37 42 40 28 47 27 41 34 50 47 53 53 47 49 29 45 38 32 70 65 56 35 56 56 49 38 35 38 49 55 43 43 25 30 46 50 35 26
## [321] 45 37 60 38 47 33 51 35 35 48 37 29 34 32 30 47 33 33 63 51 41 56 60 41 37 36 57 55 44 47 31 35 39 50 29 30 46 27 48 26
## [361] 36 42 50 49 45 23 38 32 60 30 58 60 38 51 42 30 40 37 53 27 39 39 59 37 41 45 48 48 32 35 33 28 64 35 33 41 34 37 38 39
## [401] 35 28 27 48 50 45 41 39 44 54 49 34 75 42 53 49 31 45 39 46 42 37 26 37 36 29 36 26 32 40 53 29 34 43 56 55 31 27 36 44
## [441] 37 30 42 30 62 24 31 66 47 39 35 35 27 32 44 42 34 25 40 43 55 50 45 35 32 59 34 39 40 36 40 55 38 61 41 57 48 77 35 54
## [481] 37 56 40 40 54 55 55 52 55 33 52 33 53 46 44 48 50 34 38 42 32 58 31 19 41 31 52 42 41 30 38 32 52 48 36 45 34 36 34 61
## [521] 54 50 43 47 45 57 27 49 43 40 32 58 34 46 29 52 45 26 40 41 47 27 27 48 49 35 47 54 39 28 54 34 63 42 43 37 55 43 41 47
## [561] 34 28 36 50 35 40 26 36 59 28 46 50 53 81 35 58 44 38 49 33 29 28 31 33 52 43 32 36 41 43 45 39 41 31 37 28 47 37 60 43
## [601] 58 52 50 32 42 31 28 36 51 46 50 32 26 41 39 31 28 31 30 35 33 30 32 40 31 36 33 57 45 59 30 33 52 83 58 33 41 31 35 27
## [641] 57 56 53 39 32 46 56 46 29 60 33 27 50 45 58 59 58 64 47 40 36 28 38 53 46 54 32 55 55 56 40 44 32 33 41 37 58 52 49 44
## [681] 28 31 37 30 28 46 47 35 80 30 55 22 33 53 49 51 41 34 36 35 29 40 32 48 59 35 33 55 41 42 46 35 28 25 56 30 45 40 40 39
## [721] 48 31 32 34 26 31 37 29 57 47 38 48 50 33 28 30 52 36 36 46 57 28 46 55 33 53 41 60 35 43 57 46 31 53 35 32 48 50 40 42
## [761] 59 27 32 35 28 37 57 42 55 52 26 52 26 33 24 37 28 30 33 55 33 42 37 59 27 23 51 50 43 37 34 41 26 49 29 30 38 58 34 53
## [801] 50 30 40 57 40 41 53 48 32 34 46 45 51 31 47 32 41 37 27 27 50 31 40 43 39 31 48 32 47 35 42 47 59 38 50 27 55 39 31 37
## [841] 28 42 40 59 40 43 38 54 41 55 33 46 36 48 42 46 49 36 34 51 58 48 41 49 43 32 54 33 35 56 57 31 33 29 38 37 33 30 31 28
## [881] 26 31 50 45 48 41 60 41 43 36 40 28 27 49 37 31 31 31 35 51 33 30 58 34 45 28 31 40 30 34 40 59 43 41 33 33 36 32 25 25
## [921] 38 34 67 32 45 33 38 32 54 51 42 41 39 38 39 46 50 33 37 31 34 34 36 40 40 41 52 40 48 33 44 55 51 34 58 39 27 55 35 37
## [961] 52 41 52 34 53 33 47 35 33 55 58 44 25 37 41 34 36 44 34 56 43 28 54 37 59 58 56 32 42 52 46 37 39 31 59 38 58 34 40 20
## [ reached 'max' / getOption("max.print") -- omitted 3521 entries ]