I have a dataset with hundreds of variables that is organized in wide format, like that:
df <- tibble(
subject = 1:800,
var1.W1 = sample(1:4, replace=T, 800), # var 1 measured at wave 1
var1.W2 = sample(1:4, replace=T, 800), # var 1 measured at wave 2
var1.W3 = sample(1:4, replace=T, 800), # var 1 measured at wave 3
var1.W4 = sample(1:4, replace=T, 800), # var 1 measured at wave 4
...
var_n.W1 = sample(1:4, replace=T, 800), # var n measured at wave 1
var_n.W2 = sample(1:4, replace=T, 800), # var n measured at wave 2
var_n.W3 = sample(1:4, replace=T, 800), # var n measured at wave 3
var_n.W4 = sample(1:4, replace=T, 800) # var n measured at wave 4
)
In order to transform that wide data to long data, I am doing that:
df2 <- df %>%
gather(var1, var1_value, c(var1.W1, var1.W2, var1.W3, var1.W4))
df2 <- df2 %>%
gather(var2, var2_value, c(var2.W1, var2.W2, var2.W3, var2.W4))
# Etc...
I suspect that isn't the smartest thing to do. Is there same good alternative to covert this kind of wide data to long data? (If it helps: all variables I want to gather has the same prefix and the sufix (.W1, .W2, .W3 or .W4)) point what wave it come from.