Skip to content

Instantly share code, notes, and snippets.

@debruine
Last active August 27, 2019 21:19
Show Gist options
  • Save debruine/0aaab8375e54dd3d7113a3a60963619c to your computer and use it in GitHub Desktop.
Save debruine/0aaab8375e54dd3d7113a3a60963619c to your computer and use it in GitHub Desktop.
Create a list of summary variables by group
a <- rep(c(1, 2), each = 4)
b <- c(1,2,3,4,5,6,7,8)
frame <- data.frame(a,b)
library(dplyr)
library(tidyr)
x <- frame %>%
group_by(a) %>%
summarise(Mean = mean(b),
SD = sd(b)) %>%
gather(stat, val, Mean, SD) %>%
unite(stat, stat, a, sep = "_") %>%
spread(stat, val) %>%
as.list()
list2env(x, envir = environment())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment