Last active
August 27, 2019 21:19
-
-
Save debruine/0aaab8375e54dd3d7113a3a60963619c to your computer and use it in GitHub Desktop.
Create a list of summary variables by group
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
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