Skip to content

Instantly share code, notes, and snippets.

@pachevalier
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save pachevalier/2010b7ad76be83161d50 to your computer and use it in GitHub Desktop.

Select an option

Save pachevalier/2010b7ad76be83161d50 to your computer and use it in GitHub Desktop.
A problem with spread function
library("dplyr")
library("tidyr")
set.seed(10)
messy <- data.frame(
id = 1:4,
trt = sample(rep(c('control', 'treatment'), each = 2)),
work.T1 = runif(4),
home.T1 = runif(4),
work.T2 = runif(4),
home.T2 = runif(4)
)
temp <- gather(messy, key = variable, value = "value", work.T1:home.T2)
temp2 <- separate(temp, variable, c("location", "time"), sep = "\\.")
temp2$value <- round(temp2$value,2)
temp3 <- spread(temp2, location, value)
temp3[1,]
filter(temp2, id == 1, trt == "treatment", time == "T1")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment