Skip to content

Instantly share code, notes, and snippets.

@knbknb
Last active March 1, 2018 13:24
Show Gist options
  • Save knbknb/84b53a921a00d106e39e4b1bbaba63ee to your computer and use it in GitHub Desktop.
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
# 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