Ejercicio 1
# A. Filtrar mpg > 25 y wt < 2.5
ejercicio_A <- mtcars[mtcars$mpg > 25 & mtcars$wt < 2.5, ]
ejercicio_A
# B. Ordenar por hp ascendente y seleccionar mpg, hp, wt
ejercicio_B <- mtcars %>%
arrange(hp) %>%
select(mpg, hp, wt)
ejercicio_B
# C. Crear la columna kpl
mtcars <- mtcars %>%
mutate(kpl = mpg * 0.4251)
head(mtcars)
# D. Media, mediana y desviación estándar de mpg
mtcars %>%
summarise(
media = mean(mpg),
mediana = median(mpg),
desviacion_estandar = sd(mpg)
)
# E. Agrupar por cyl y calcular media y desviación estándar de mpg
mtcars %>%
group_by(cyl) %>%
summarise(
media_mpg = mean(mpg),
sd_mpg = sd(mpg)
)
Ejercicio 2
if(!require(pwt10)) install.packages("pwt10")
library(pwt10)
library(dplyr)
pwt <- pwt10::pwt10.01
iso_latam <- c("ARG", "BOL", "BRA", "CHL", "COL", "CRI", "CUB", "DOM",
"ECU", "SLV", "GTM", "HND", "MEX", "NIC", "PAN", "PRY",
"PER", "URY", "VEN")
# A. Filtrar solo países de América Latina
pwt_latam <- pwt %>%
filter(isocode %in% iso_latam)
# B. Promedio de Ingreso Per Cápita e Índice de Capital Humano (Año 2019)
# Nota: rgdpe está en millones, pop en millones -> rgdpe/pop da miles de dólares per cápita
pwt_latam %>%
filter(year == 2019) %>%
summarise(
ingreso_pc_promedio_2019 = mean(rgdpe / pop, na.rm = TRUE),
hc_promedio_2019 = mean(hc, na.rm = TRUE)
)
# C. Clasificar países por nivel de ingreso (Banco Mundial)
# Se calcula el ingreso per cápita en dólares (multiplicando por 1000)
pwt_latam <- pwt_latam %>%
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"
)
)
# 2019
pwt_latam_2019 <- pwt_latam %>%
filter(year == 2019) %>%
select(country, isocode, year, ingreso_pc, nivel_ingreso)
pwt_latam_2019
# D. Crecimiento promedio del PIB real (rgdpe) por grupo de ingreso (2015-2019)
crecimiento_por_grupo <- pwt_latam %>%
filter(year %in% c(2015, 2019)) %>%
group_by(isocode, country) %>%
summarise(
nivel_ingreso_2015 = nivel_ingreso[year == 2015],
pib_2015 = rgdpe[year == 2015],
pib_2019 = rgdpe[year == 2019],
crecimiento_pct = ((pib_2019 - pib_2015) / pib_2015) * 100,
.groups = "drop"
) %>%
group_by(nivel_ingreso_2015) %>%
summarise(
num_paises = n(),
crecimiento_promedio_pib_pct = mean(crecimiento_pct, na.rm = TRUE)
)
crecimiento_por_grupo