Last active
June 7, 2019 20:29
-
-
Save debruine/170842398583b67a0a0dfe756794a162 to your computer and use it in GitHub Desktop.
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
library(faux) | |
library(tidyverse) | |
data <- sim_design(within = list(T = c("T1", "T2", "T3", "T4", "T5")), | |
id = "ID") | |
processed_data <- data %>% | |
gather(T, raw, T1:T5) %>% # put in long format | |
group_by(ID) %>% # start doing calculations within groups | |
mutate(Mean_T = mean(raw), | |
Variance_T = var(raw), | |
Centered = raw - Mean_T) %>% | |
ungroup() %>% # stop doing calculations within groups | |
# reshape data to wide format | |
gather(score_type, val, c(raw, Centered)) %>% # make long | |
unite(var, score_type, T) %>% # new col "var" = raw/centered + T cols | |
spread(var, val) # make wide | |
## more variables (5w*2w design) | |
data <- sim_design(within = list(T = c("T1", "T2", "T3", "T4", "T5"), | |
condition = c("A", "B")), | |
id = "ID") | |
processed_data <- data %>% | |
gather(var, raw, T1_A:T5_B) %>% # put in long format | |
separate(var, c("time", "condition")) %>% | |
group_by(ID, condition) %>% # start doing calculations within groups | |
mutate(Mean_T = mean(raw), | |
Variance_T = var(raw), | |
Centered = raw - Mean_T) %>% | |
ungroup() %>% # stop doing calculations within groups | |
select(-raw) %>% # remove raw values | |
spread(time, Centered) %>% # spread to 1 row per ID/condition | |
gather(var, val, Mean_T:T5) %>% # gather back to long | |
unite(var, var, condition) %>% # merge condition into var label | |
spread(var, val) # spread back out |
This works great! The last Q for the ww dataset is how to spread back into ww?
spread(time, Centered)
still has condition in the long format?
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you don't need the raw scores anymore, you can replace
with