Last active
March 1, 2018 13:24
-
-
Save knbknb/84b53a921a00d106e39e4b1bbaba63ee to your computer and use it in GitHub Desktop.
R code: # group_by / nest / mutate /unnest ## demonstrates an idiom that I tend to forget the details of
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from the datacamp course | |
# 'Exploratory Data Analysis in R: Case Study' | |
# by David Robinson | |
# | |
# this gist demonstrates an idiom that I tend to forget the details of | |
# group_by / nest / model /unnest | |
# | |
# Load purrr, tidyr, and broom | |
library(purrr) | |
library(tidyr) | |
library(broom) | |
# by_country_year_topic is already grouped | |
by_country_year_topic | |
# Fit model on the by_country_year_topic dataset | |
country_topic_coefficients <- by_country_year_topic %>% | |
nest(-country, -topic) %>% | |
mutate(model = map(data, ~ lm(percent_yes ~ year, data = .)), | |
tidied = map(model, tidy)) %>% | |
unnest(tidied) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment