UNIVERSIDAD CENTRAL DEL ECUADOR

FACULTAD DE CIENCIAS ECONÓMICAS

INGENIERIA EN ESTADÍSTICA

APLICANDO CONOCIMIENTOS ADQUIRIDOS ACERCA DE TRATAMIENTO DE ARCHIVOS

En este documento se presentan 5 ejercicios de aprendizaje sobre el tratamiento de archivos, para la comunidad de RStudio.

AUTORES

COLABORACIÓN

EJERCICIOS

EJERCICIO 1

Lee el fichero paro.csv (ARCHIVO ADJUNTO) usando la función read.table. Comprueba que está correctamente importado usando head, tail, nrow, summary, etc. Para leer la tabla necesitarás leer con cierto detenimiento ?read.table.

# getwd()
setwd("C:/GABY")
tabla1 <- read.table("C:/GABY/paro.csv",header = T)
View(tabla1)

head(tabla1)
##    Gender           Provinces Periodo Situation value
## 1   Males         02 Albacete  2014Q4    active 103.9
## 2 Females         02 Albacete  2014Q4    active  83.5
## 3   Males 03 Alicante/Alacant  2014Q4    active 490.5
## 4 Females 03 Alicante/Alacant  2014Q4    active 398.7
## 5   Males          04 Almería  2014Q4    active 198.5
## 6 Females          04 Almería  2014Q4    active 155.8
tail(tabla1)
##       Gender   Provinces Periodo Situation value
## 8315   Males 50 Zaragoza  2011Q1  inactive 134.3
## 8316 Females 50 Zaragoza  2011Q1  inactive 199.4
## 8317   Males    51 Ceuta  2011Q1  inactive  10.7
## 8318 Females    51 Ceuta  2011Q1  inactive  16.5
## 8319   Males  52 Melilla  2011Q1  inactive   9.1
## 8320 Females  52 Melilla  2011Q1  inactive  15.3
nrow(tabla1)
## [1] 8320
summary(tabla1)
##     Gender           Provinces           Periodo           Situation        
##  Length:8320        Length:8320        Length:8320        Length:8320       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##      value       
##  Min.   :   0.0  
##  1st Qu.:  14.4  
##  Median :  61.9  
##  Mean   : 120.1  
##  3rd Qu.: 144.7  
##  Max.   :1828.9  
##  NA's   :1

EJERCICIO 2

Repite el ejercicio anterior eliminando la opción header = TRUE. Examina el resultado y comprueba que, efectivamente, los datos no se han cargado correctamente.

setwd("C:/GABY")
tabla1 <- read.table("C:/GABY/paro.csv",header = F)
View(tabla1)

head(tabla1)
##        V1                  V2      V3        V4    V5
## 1  Gender           Provinces Periodo Situation value
## 2   Males         02 Albacete  2014Q4    active 103.9
## 3 Females         02 Albacete  2014Q4    active  83.5
## 4   Males 03 Alicante/Alacant  2014Q4    active 490.5
## 5 Females 03 Alicante/Alacant  2014Q4    active 398.7
## 6   Males          04 Almería  2014Q4    active 198.5
tail(tabla1)
##           V1          V2     V3       V4    V5
## 8316   Males 50 Zaragoza 2011Q1 inactive 134.3
## 8317 Females 50 Zaragoza 2011Q1 inactive 199.4
## 8318   Males    51 Ceuta 2011Q1 inactive  10.7
## 8319 Females    51 Ceuta 2011Q1 inactive  16.5
## 8320   Males  52 Melilla 2011Q1 inactive   9.1
## 8321 Females  52 Melilla 2011Q1 inactive  15.3
nrow(tabla1)
## [1] 8321
summary(tabla1)
##       V1                 V2                 V3                 V4           
##  Length:8321        Length:8321        Length:8321        Length:8321       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##       V5           
##  Length:8321       
##  Class :character  
##  Mode  :character

EJERCICIO 3

Lee algún fichero de datos de tu interés y repite el ejercicio anterior.

setwd("C:/GABY")
tabla2 <- readxl::read_xlsx("C:/GABY/EDF_2020.xlsx")
tabla2
## # A tibble: 1,222 × 11
##    prov_insc   cant_…¹ parr_…² anio_…³ mes_i…⁴ dia_i…⁵ fecha_insc          sexo 
##    <chr>       <chr>   <chr>     <dbl> <chr>     <dbl> <dttm>              <chr>
##  1 Pichincha   Quito   San Ju…    2020 Febrero      21 2020-02-21 00:00:00 Homb…
##  2 Santa Elena Santa … Santa …    2020 Septie…      29 2020-09-29 00:00:00 Mujer
##  3 Manabí      Portov… 18 De …    2020 Septie…       7 2020-09-07 00:00:00 Mujer
##  4 Pichincha   Quito   Iñaqui…    2020 Febrero       4 2020-02-04 00:00:00 Mujer
##  5 Pichincha   Mejia   Machac…    2020 Septie…       7 2020-09-07 00:00:00 Mujer
##  6 Azuay       Cuenca  San Bl…    2020 Febrero      20 2020-02-20 00:00:00 Homb…
##  7 Azuay       Cuenca  San Bl…    2020 Enero         7 2020-01-07 00:00:00 Homb…
##  8 Azuay       Gualac… Gualac…    2020 Enero        17 2020-01-17 00:00:00 Homb…
##  9 Azuay       Cuenca  El Bat…    2020 Enero         2 2020-01-02 00:00:00 Mujer
## 10 Azuay       Cuenca  El Bat…    2020 Enero         2 2020-01-02 00:00:00 Mujer
## # … with 1,212 more rows, 3 more variables: peso <chr>, sem_gest <chr>,
## #   talla <dbl>, and abbreviated variable names ¹​cant_insc, ²​parr_insc,
## #   ³​anio_insc, ⁴​mes_insc, ⁵​dia_insc
## # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names
head(tabla2)
## # A tibble: 6 × 11
##   prov_insc   cant_i…¹ parr_…² anio_…³ mes_i…⁴ dia_i…⁵ fecha_insc          sexo 
##   <chr>       <chr>    <chr>     <dbl> <chr>     <dbl> <dttm>              <chr>
## 1 Pichincha   Quito    San Ju…    2020 Febrero      21 2020-02-21 00:00:00 Homb…
## 2 Santa Elena Santa E… Santa …    2020 Septie…      29 2020-09-29 00:00:00 Mujer
## 3 Manabí      Portovi… 18 De …    2020 Septie…       7 2020-09-07 00:00:00 Mujer
## 4 Pichincha   Quito    Iñaqui…    2020 Febrero       4 2020-02-04 00:00:00 Mujer
## 5 Pichincha   Mejia    Machac…    2020 Septie…       7 2020-09-07 00:00:00 Mujer
## 6 Azuay       Cuenca   San Bl…    2020 Febrero      20 2020-02-20 00:00:00 Homb…
## # … with 3 more variables: peso <chr>, sem_gest <chr>, talla <dbl>, and
## #   abbreviated variable names ¹​cant_insc, ²​parr_insc, ³​anio_insc, ⁴​mes_insc,
## #   ⁵​dia_insc
## # ℹ Use `colnames()` to see all variable names
tail(tabla2)
## # A tibble: 6 × 11
##   prov_insc cant_insc parr_i…¹ anio_…² mes_i…³ dia_i…⁴ fecha_insc          sexo 
##   <chr>     <chr>     <chr>      <dbl> <chr>     <dbl> <dttm>              <chr>
## 1 Pichincha Quito     Caldero…    2020 Septie…       7 2020-09-07 00:00:00 Homb…
## 2 Pichincha Quito     Caldero…    2020 Septie…      14 2020-09-14 00:00:00 Mujer
## 3 Pichincha Quito     Quitumbe    2020 Julio        27 2020-07-27 00:00:00 Mujer
## 4 Pichincha Quito     Itchimb…    2020 Julio        27 2020-07-27 00:00:00 Homb…
## 5 Pichincha Quito     Itchimb…    2020 Julio        27 2020-07-27 00:00:00 Mujer
## 6 Pichincha Quito     Itchimb…    2020 Agosto       24 2020-08-24 00:00:00 Homb…
## # … with 3 more variables: peso <chr>, sem_gest <chr>, talla <dbl>, and
## #   abbreviated variable names ¹​parr_insc, ²​anio_insc, ³​mes_insc, ⁴​dia_insc
## # ℹ Use `colnames()` to see all variable names
nrow(tabla2)
## [1] 1222
summary(tabla2)
##   prov_insc          cant_insc          parr_insc           anio_insc   
##  Length:1222        Length:1222        Length:1222        Min.   :2020  
##  Class :character   Class :character   Class :character   1st Qu.:2020  
##  Mode  :character   Mode  :character   Mode  :character   Median :2020  
##                                                           Mean   :2020  
##                                                           3rd Qu.:2020  
##                                                           Max.   :2020  
##    mes_insc            dia_insc      fecha_insc                    
##  Length:1222        Min.   : 1.0   Min.   :2020-01-01 00:00:00.00  
##  Class :character   1st Qu.: 7.0   1st Qu.:2020-03-09 00:00:00.00  
##  Mode  :character   Median :15.0   Median :2020-06-12 00:00:00.00  
##                     Mean   :15.2   Mean   :2020-06-15 04:55:46.64  
##                     3rd Qu.:23.0   3rd Qu.:2020-09-09 00:00:00.00  
##                     Max.   :31.0   Max.   :2020-12-31 00:00:00.00  
##      sexo               peso             sem_gest             talla      
##  Length:1222        Length:1222        Length:1222        Min.   : 0.00  
##  Class :character   Class :character   Class :character   1st Qu.:30.00  
##  Mode  :character   Mode  :character   Mode  :character   Median :39.00  
##                                                           Mean   :38.39  
##                                                           3rd Qu.:47.75  
##                                                           Max.   :55.00

EJERCICIO 4

En read.table y sus derivados puedes indicar, además de ficheros disponibles en el disco duro, la URL de uno disponible en internet. Prueba a leer directamente el fichero disponible en https://datanalytics.com/uploads/datos_treemap.txt. Nota: es un fichero de texto separado por tabuladores y con nombres de columna.

url <- "https://datanalytics.com/uploads/datos_treemap.txt"
datos <- read.table(url, header = T)
datos
##    valor   cap   div
## 1    ABE  7793 12.04
## 2    ABG  1473  1.23
## 3    ACS  7930  8.13
## 4    ACX  2217  5.06
## 5    AMS  5910  2.27
## 6    ANA  3760  5.24
## 7   BBVA 24819  8.34
## 8    BKT  1646  5.85
## 9    BME  1589 10.38
## 10  CABK 11644  6.48
## 11   ELE 17215  3.08
## 12   ENG  3092  6.47
## 13   EVA  1971  6.70
## 14   FCC  2026  8.99
## 15   FER  5780  5.33
## 16   GAM   838  0.20
## 17   GAS 11022  7.24
## 18   GRF  2978    NA
## 19   IBE 25989  7.51
## 20  ICAG  3193    NA
## 21   IDR  1904  5.86
## 22   ITX 36571  2.39
## 23   MAP  6446  7.17
## 24   MTS 20577  1.82
## 25   OHL  1721  2.84
## 26   POP  4392  5.58
## 27   REE  4239  5.98
## 28   REP 22885  5.60
## 29   SAB  3433  5.67
## 30   SAN 44430 11.38
## 31   SYV  1823  2.32
## 32   TEF 57917 11.03
## 33   TL5  1652  8.62
## 34   TRE  1336  5.61

EJERCICIO 5

Alternativamente, si quieres leer un fichero remoto, puedes descargarlo directamente desde R. Consulta la ayuda de download.file para bajarte al disco duro el fichero del ejercicio anterior (EJERCICIO 5) y leerlo.

web1 <- ("https://datanalytics.com/uploads/datos_treemap.txt")
download.file(web1,"datos_treemap.txt")
View(web1)

setwd("C:/GABY")
tabla3 <- read.table("C:/GABY/datos_treemap.txt",header = T)
tabla3
##    valor   cap   div
## 1    ABE  7793 12.04
## 2    ABG  1473  1.23
## 3    ACS  7930  8.13
## 4    ACX  2217  5.06
## 5    AMS  5910  2.27
## 6    ANA  3760  5.24
## 7   BBVA 24819  8.34
## 8    BKT  1646  5.85
## 9    BME  1589 10.38
## 10  CABK 11644  6.48
## 11   ELE 17215  3.08
## 12   ENG  3092  6.47
## 13   EVA  1971  6.70
## 14   FCC  2026  8.99
## 15   FER  5780  5.33
## 16   GAM   838  0.20
## 17   GAS 11022  7.24
## 18   GRF  2978    NA
## 19   IBE 25989  7.51
## 20  ICAG  3193    NA
## 21   IDR  1904  5.86
## 22   ITX 36571  2.39
## 23   MAP  6446  7.17
## 24   MTS 20577  1.82
## 25   OHL  1721  2.84
## 26   POP  4392  5.58
## 27   REE  4239  5.98
## 28   REP 22885  5.60
## 29   SAB  3433  5.67
## 30   SAN 44430 11.38
## 31   SYV  1823  2.32
## 32   TEF 57917 11.03
## 33   TL5  1652  8.62
## 34   TRE  1336  5.61

EL CONOCIMIENTO ES LA GUÍA PARA FORJAR UN FUTURO MEJOR