UNIVERSIDAD CENTRAL DEL ECUADOR

Facultad de Ciencias Económicas

Estadística

Autores: Fernanda Chipantaxi, Ayleen Rubio, Marlon Llugsha, Cristhian Diaz

Fecha: 23-07-2022

Colaborador: Ing. Francisco Valverde, PHD en informática

EJERCICIOS DE EXPORTACIÓN E IMPORTACIÓN DE ARCHIVOS EN R

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.

PARO <- read.table("C:/Taller9/paro.csv",header = T)
head(PARO) 
##    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(PARO) 
##       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(PARO) 
## [1] 8320
ncol(PARO) 
## [1] 5
summary(PARO) 
##     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.

PARO <- read.table("C:/Taller9/paro.csv",header = F)
head(PARO) 
##        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(PARO) 
##           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(PARO) 
## [1] 8321
ncol(PARO) 
## [1] 5
summary(PARO) 
##       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

GENERO <- read.csv("C:/Taller9/genero2005.csv",header = T, sep = ",")

head(GENERO) 
##                                     Pais mort.mat gasto.sal parto.inst alfa.muj
## 1 Kuwait                                        9       538         98     91.0
## 2 Namibia                                      46       407         76     83.5
## 3 Angola                                      154        38         45     54.2
## 4 Cape Verde                                   26       225         89     75.5
## 5 Mozambique                                  100        42         48     25.0
## 6 Jordan                                       22       502        100     87.0
##   parl.muj dgen.ing dgen.sup muj.pea voto.muj conv.muj
## 1      3.1     0.35       29    49.0     2005     1994
## 2     26.9     0.57        7    46.6     1989     1992
## 3     15.0     0.62        1    73.7     1975     1986
## 4     15.3     0.35        7    34.0     1975     1980
## 5     34.8     0.81        1    84.5     1975     1997
## 6      7.9     0.31       40    27.5     1974     1992
tail(GENERO) 
##                                      Pais mort.mat gasto.sal parto.inst
## 70 Azerbaijan                                   74       138         88
## 71 Belarus                                      10       427        100
## 72 Estonia                                       6       752        100
## 73 Kyrgyzstan                                   58       102         98
## 74 Latvia                                        9       852        100
## 75 Russian Federation                           14       583         99
##    alfa.muj parl.muj dgen.ing dgen.sup muj.pea voto.muj conv.muj
## 70     98.2     11.3     0.65       14    60.2     1918     1995
## 71     99.4     29.8     0.63       72    52.5     1918     1981
## 72     99.8     21.8     0.62       82    52.3     1918     1991
## 73     98.1      0.0     0.58       46    55.0     1918     1997
## 74     99.7     19.0     0.65       96    49.0     1918     1992
## 75     99.2      8.0     0.62       82    54.3     1918     1981
nrow(GENERO) 
## [1] 75
ncol(GENERO) 
## [1] 11
summary(GENERO) 
##      Pais              mort.mat        gasto.sal       parto.inst    
##  Length:75          Min.   :  6.00   Min.   : 16.0   Min.   :  6.00  
##  Class :character   1st Qu.: 17.00   1st Qu.: 91.0   1st Qu.: 56.00  
##  Mode  :character   Median : 31.00   Median :229.0   Median : 85.00  
##                     Mean   : 50.35   Mean   :293.2   Mean   : 74.29  
##                     3rd Qu.: 78.50   3rd Qu.:427.0   3rd Qu.: 98.00  
##                     Max.   :165.00   Max.   :917.0   Max.   :100.00  
##     alfa.muj        parl.muj        dgen.ing         dgen.sup    
##  Min.   :15.10   Min.   : 0.00   Min.   :0.2500   Min.   : 1.00  
##  1st Qu.:60.50   1st Qu.: 9.40   1st Qu.:0.4100   1st Qu.: 2.50  
##  Median :83.50   Median :16.70   Median :0.5300   Median :15.00  
##  Mean   :74.75   Mean   :17.05   Mean   :0.5219   Mean   :24.03  
##  3rd Qu.:95.35   3rd Qu.:22.75   3rd Qu.:0.6300   3rd Qu.:35.50  
##  Max.   :99.80   Max.   :45.30   Max.   :0.8100   Max.   :96.00  
##     muj.pea         voto.muj       conv.muj   
##  Min.   :26.80   Min.   :1918   Min.   :1980  
##  1st Qu.:45.10   1st Qu.:1943   1st Qu.:1982  
##  Median :52.30   Median :1953   Median :1987  
##  Mean   :53.75   Mean   :1950   Mean   :1988  
##  3rd Qu.:60.75   3rd Qu.:1960   3rd Qu.:1994  
##  Max.   :91.80   Max.   :2005   Max.   :2004

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.

web1 <- "https://datanalytics.com/uploads/datos_treemap.txt"
web2 <- read.table(web1,header = T,sep = "\t")
web2
##    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 después.

web3 <- "https://datanalytics.com/uploads/datos_treemap.txt"
download.file(web3, "datos_treemap.txt")
web4 <- read.table(file = "datos_treemap.txt", header = T)
web4
##    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