gm_nest <- gapminder::gapminder %>% tidyr::nest(country_data = -continent)
# add or modify columns:
gm_nest %>%
nest_mutate(
country_data,
lifeExp = NULL,
gdp = gdpPercap * pop,
pop = pop/1000000
)
# use dplyr::across() to apply transformation to multiple columns
gm_nest %>%
nest_mutate(
country_data,
across(c(lifeExp:gdpPercap), mean)
)
# nest_transmute() drops unused columns when mutating:
gm_nest %>%
nest_transmute(
country_data,
country = country,
year = year,
pop = pop/1000000
)
Run the code above in your browser using DataLab