library(moderndive)
library(dplyr)
# Single explanatory variable:
un_member_states_2024 %>%
get_correlation(formula = life_expectancy_2022 ~ gdp_per_capita, na.rm = TRUE)
# Multiple explanatory variables — long format:
un_member_states_2024 %>%
get_correlation(
formula = life_expectancy_2022 ~ gdp_per_capita + fertility_rate_2022 + hdi_2022,
na.rm = TRUE,
quiet = TRUE
)
# Multiple explanatory variables — wide format:
un_member_states_2024 %>%
get_correlation(
formula = life_expectancy_2022 ~ gdp_per_capita + fertility_rate_2022 + hdi_2022,
wide = TRUE,
na.rm = TRUE,
quiet = TRUE
)
# Group by one variable:
un_member_states_2024 %>%
group_by(continent) %>%
get_correlation(formula = life_expectancy_2022 ~ gdp_per_capita, na.rm = TRUE)
# Group by two variables:
un_member_states_2024 %>%
group_by(continent, income_group_2024) %>%
get_correlation(formula = life_expectancy_2022 ~ gdp_per_capita, na.rm = TRUE)
Run the code above in your browser using DataLab