Skip to content

Instantly share code, notes, and snippets.

@bryanyang0528
Created August 18, 2014 13:30
Show Gist options
  • Save bryanyang0528/6a0d2fb1b4261a0f5246 to your computer and use it in GitHub Desktop.
Save bryanyang0528/6a0d2fb1b4261a0f5246 to your computer and use it in GitHub Desktop.
R ddplyr example 1
## The code for the toy data is exactly the same
data <- data.frame(sex = c(rep(1, 1000), rep(2, 1000)),
treatment = rep(c(1, 2), 1000),
response1 = rnorm(2000, 0, 1),
response2 = rnorm(2000, 0, 1))
## reshape2 still does its thing:
library(reshape2)
melted <- melt(data, id.vars=c("sex", "treatment"))
## This part is new:
library(dplyr)
grouped <- group_by(melted, sex, treatment)
summarise(grouped, mean=mean(value), sd=sd(value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment