Created
April 27, 2020 13:19
-
-
Save lpsatchell/1d51a98a6c03b1fe0872cd5df62911d8 to your computer and use it in GitHub Desktop.
Multiple variable gather
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
#Wide data frame has three time points where participants answer two questions on two topics. | |
#Needs to become a long data frame for time, but independent columnsfor topic and question. | |
#The clunky way to do this is my solution below. Is there a more efficient solution? | |
library(dplyr) | |
library(tidyr) | |
#Simmed data | |
Time1.Topic1.Question1 <- rnorm(500) | |
data <- data.frame(Time1.Topic1.Question1) | |
data$Time1.TOpic1.Question2 <- rnorm(500) | |
data$Time1.Topic2.Question1 <- rnorm(500) | |
data$Time1.Topic2.Question2 <- rnorm(500) | |
data$Time2.Topic1.Question1 <- rnorm(500) | |
data$Time2.Topic1.Question2 <- rnorm(500) | |
data$Time2.Topic2.Question1 <- rnorm(500) | |
data$Time2.Topic2.Question2 <- rnorm(500) | |
data$Time3.Topic1.Question1 <- rnorm(500) | |
data$Time3.Topic1.Question2 <- rnorm(500) | |
data$Time3.Topic2.Question1 <- rnorm(500) | |
data$Time3.Topic2.Question2 <- rnorm(500) | |
data <- tibble::rowid_to_column(data, "Id") | |
data$Condition <- sample(x = 1:5, size=500, replace=TRUE) | |
#Starting data | |
#View(data) | |
#Pull out sections and long-ify Topics | |
data %>% select(Id, ends_with("Topic1.Question1")) -> T1.Q1.data | |
T1.Q1.data <- gather(T1.Q1.data, Time, T1Q1, 2:4, factor_key = TRUE) | |
data %>% select(Id, ends_with("Topic1.Question2")) -> T1.Q2.data | |
T1.Q2.data <- gather(T1.Q2.data, Time, T1Q2, 2:4, factor_key = TRUE) | |
data %>% select(Id, ends_with("Topic2.Question1")) -> T2.Q1.data | |
T2.Q1.data <- gather(T2.Q1.data, Time, T2Q1, 2:4, factor_key = TRUE) | |
data %>% select(Id, ends_with("Topic2.Question2")) -> T2.Q2.data | |
T2.Q2.data <- gather(T2.Q2.data, Time, T2Q2, 2:4, factor_key = TRUE) | |
#Create a time coded variable that doesn't contain mess of column title names | |
data %>% select(Id) -> time.data | |
time.data$Baseline <- 0 | |
time.data$TimeOne <- 1 | |
time.data$Timetwo <- 2 | |
time.data <- gather(time.data, Time, Timepoint, 2:4, factor_key = TRUE) | |
#Drop uneededs | |
time.data %>% select(-(Time)) -> time.data | |
T1.Q1.data %>% select(-(Time)) -> T1.Q1.data | |
T1.Q2.data %>% select(-(Time)) -> T1.Q2.data | |
T2.Q1.data %>% select(-(Time)) -> T2.Q1.data | |
T2.Q2.data %>% select(-(Time)) -> T2.Q2.data | |
#Merge | |
data %>% select(Id, Condition) -> idcond.data | |
longdata <- merge(idcond.data,time.data,by="Id") | |
longdata <- merge(longdata,T1.Q1.data,by="Id") | |
longdata <- merge(longdata,T1.Q2.data,by="Id") | |
longdata <- merge(longdata,T2.Q1.data,by="Id") | |
longdata <- merge(longdata,T2.Q2.data,by="Id") | |
#View | |
#View(longdata) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment