Paquetes
library(dplyr)
library(knitr)
library(kableExtra)
## Warning: package 'kableExtra' was built under R version 4.4.3
library(DT)
## Warning: package 'DT' was built under R version 4.4.3
# Mostrar números completos (sin notación científica)
options(scipen=999)
Ejercicio 1: Uso del dataframe mtcars
Variables del dataset
El dataset mtcars contiene 32 observaciones de
automóviles y las siguientes variables:
| mpg |
Consumo: millas por galón |
mpg |
| cyl |
Número de cilindros |
entero |
| disp |
Desplazamiento del motor |
pulgadas³ |
| hp |
Potencia |
caballos de fuerza |
| drat |
Relación eje trasero |
— |
| wt |
Peso del automóvil |
miles de libras |
| qsec |
Tiempo 1/4 milla |
segundos |
| vs |
Motor (0 = V, 1 = recto) |
categórico |
| am |
Transmisión (0 = auto, 1 = manual) |
categórico |
| gear |
Número de marchas |
entero |
| carb |
Número de carburadores |
entero |
a) Autos con mpg > 25 y wt < 2.5
ej1_a <- mtcars %>% filter(mpg > 25, wt < 2.5)
ej1_a %>%
kable(caption = "Autos con consumo > 25 mpg y peso < 2.5") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = FALSE)
Autos con consumo > 25 mpg y peso < 2.5
|
|
mpg
|
cyl
|
disp
|
hp
|
drat
|
wt
|
qsec
|
vs
|
am
|
gear
|
carb
|
|
Fiat 128
|
32.4
|
4
|
78.7
|
66
|
4.08
|
2.200
|
19.47
|
1
|
1
|
4
|
1
|
|
Honda Civic
|
30.4
|
4
|
75.7
|
52
|
4.93
|
1.615
|
18.52
|
1
|
1
|
4
|
2
|
|
Toyota Corolla
|
33.9
|
4
|
71.1
|
65
|
4.22
|
1.835
|
19.90
|
1
|
1
|
4
|
1
|
|
Fiat X1-9
|
27.3
|
4
|
79.0
|
66
|
4.08
|
1.935
|
18.90
|
1
|
1
|
4
|
1
|
|
Porsche 914-2
|
26.0
|
4
|
120.3
|
91
|
4.43
|
2.140
|
16.70
|
0
|
1
|
5
|
2
|
|
Lotus Europa
|
30.4
|
4
|
95.1
|
113
|
3.77
|
1.513
|
16.90
|
1
|
1
|
5
|
2
|
b) Ordenar autos por hp y mostrar mpg, hp y wt
ej1_b <- mtcars %>%
arrange(hp) %>%
select(mpg, hp, wt)
datatable(head(ej1_b, 10),
caption = "Autos ordenados por potencia (primeras 10 filas)",
options = list(pageLength = 10))
c) Nueva columna kpl
ej1_c <- mtcars %>% mutate(kpl = mpg * 0.4251)
ej1_c %>%
select(mpg, kpl) %>%
head() %>%
kable(caption = "Consumo en millas por galón y en km/L (primeras filas)") %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = FALSE)
Consumo en millas por galón y en km/L (primeras filas)
|
|
mpg
|
kpl
|
|
Mazda RX4
|
21.0
|
8.92710
|
|
Mazda RX4 Wag
|
21.0
|
8.92710
|
|
Datsun 710
|
22.8
|
9.69228
|
|
Hornet 4 Drive
|
21.4
|
9.09714
|
|
Hornet Sportabout
|
18.7
|
7.94937
|
|
Valiant
|
18.1
|
7.69431
|
d) Estadísticos descriptivos de mpg
ej1_d <- mtcars %>%
summarise(
media_mpg = mean(mpg),
mediana_mpg = median(mpg),
sd_mpg = sd(mpg)
)
ej1_d %>%
kable(caption = "Media, Mediana y Desviación Estándar de mpg") %>%
kable_styling(full_width = FALSE, position = "center")
Media, Mediana y Desviación Estándar de mpg
|
media_mpg
|
mediana_mpg
|
sd_mpg
|
|
20.09062
|
19.2
|
6.026948
|
Ejercicio 2: Uso del dataframe pwt
# install.packages("pwt10") # si no está instalado
library(pwt10)
pwt <- pwt10.01
a) Seleccionar países de América Latina
latam_iso <- c("ARG","BOL","BRA","CHL","COL","CRI","CUB","DOM","ECU","SLV",
"GTM","HND","MEX","NIC","PAN","PRY","PER","URY","VEN")
pwt_latam <- pwt %>% filter(isocode %in% latam_iso)
datatable(head(pwt_latam, 10),
caption = "Primeras filas de pwt_latam",
options = list(pageLength = 10))
b) Ingreso per cápita y capital humano promedio en 2019
ej2_b <- pwt_latam %>%
filter(year == 2019) %>%
summarise(
ingreso_pc = mean(rgdpe/pop, na.rm = TRUE),
hc_prom = mean(hc, na.rm = TRUE)
)
ej2_b %>%
kable(caption = "Ingreso per cápita promedio y capital humano en 2019") %>%
kable_styling(full_width = FALSE)
Ingreso per cápita promedio y capital humano en 2019
|
ingreso_pc
|
hc_prom
|
|
14035.52
|
2.718799
|
c) Clasificación por nivel de ingreso (Banco Mundial)
ej2_c <- pwt_latam %>%
filter(year == 2019) %>%
mutate(
ingreso_pc = rgdpe/pop,
nivel_ingreso = case_when(
ingreso_pc < 1026 ~ "Bajo",
ingreso_pc >= 1026 & ingreso_pc <= 3995 ~ "Bajo-Medio",
ingreso_pc >= 3996 & ingreso_pc <= 12375 ~ "Medio-Alto",
ingreso_pc > 12375 ~ "Alto"
)
) %>%
group_by(nivel_ingreso) %>%
summarise(paises = n_distinct(isocode))
ej2_c %>%
kable(caption = "Número de países latinoamericanos por nivel de ingreso") %>%
kable_styling(full_width = FALSE, bootstrap_options = c("hover", "condensed"))
Número de países latinoamericanos por nivel de ingreso
|
nivel_ingreso
|
paises
|
|
Alto
|
10
|
|
Bajo
|
1
|
|
Medio-Alto
|
7
|
d) Crecimiento promedio del PIB real (2015-2019) por grupo de
ingreso
ej2_d <- pwt_latam %>%
filter(year %in% 2015:2019) %>%
mutate(
ingreso_pc = rgdpe/pop,
nivel_ingreso = case_when(
ingreso_pc < 1026 ~ "Bajo",
ingreso_pc >= 1026 & ingreso_pc <= 3995 ~ "Bajo-Medio",
ingreso_pc >= 3996 & ingreso_pc <= 12375 ~ "Medio-Alto",
ingreso_pc > 12375 ~ "Alto"
)
) %>%
group_by(nivel_ingreso, year) %>%
summarise(rgdpe_prom = mean(rgdpe, na.rm = TRUE), .groups = "drop") %>%
group_by(nivel_ingreso) %>%
summarise(crec_promedio = mean((rgdpe_prom/lag(rgdpe_prom)-1)*100, na.rm=TRUE))
ej2_d %>%
kable(caption = "Crecimiento promedio del PIB real (2015-2019) por nivel de ingreso") %>%
kable_styling(full_width = FALSE, bootstrap_options = c("striped", "hover"))
Crecimiento promedio del PIB real (2015-2019) por nivel de ingreso
|
nivel_ingreso
|
crec_promedio
|
|
Alto
|
-0.1406012
|
|
Bajo
|
-18.4635665
|
|
Bajo-Medio
|
NaN
|
|
Medio-Alto
|
-5.4379694
|