So I have figured out a solution on how I can transform my data from wide to long. The dataset had 190 columns representing 19 variables for 10 years (and additionally 31 columns that did not require transformation).I did it through rbinid(ddply) from the plyr package.
It is very heavy and very long.
I tried this solution (among others), but the syntax is off
My data:
DF <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 25L,25L, 25L), .Label
= c(1:1000), class = "character"),
X = c(809L, 81L, 862L,747L, 224L, 721L, 310L, 53L, 853L, 642L),
"X (- 1 år)" = c(926L, 409L,825L, 702L, 803L, 63L, 319L, 941L, 598L, 830L),
Y = c(447L,164L, 8L, 775L, 471L, 196L, 30L, 420L, 47L, 327L),
"Y (- 1 år)" = c(335L,164L, 503L, 407L, 662L, 139L, 111L, 721L, 340L, 178L),
Year = c(2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021),
"Year (- 1år)" = c(2020, 2020, 2020, 2020, 2020, 2020, 2020,2020, 2020, 2020),
bcat = c("yes", "no","yes", "no","yes", "yes","yes", "no","yes", "no")), .Names = c("id","X",
"X (- 1 år)", "Y", "Y (- 1 år)", "Year", "Year (-1 år)", "bcat"), class = "data.frame", row.names = c(1L,2L, 3L, 4L, 5L, 6L, 7L, 98L, 99L, 100L))
DF$id <- as.character(sample(100, size = nrow(DF), replace= TRUE))
Code that needs fitting:
reshape (DF,
direction = "long",
varying = list(names(DF) [2:5]),
v.names = "Y", "X",
idvar = c("id"),
timevar = "Year", "Year (-1 år)",
times = 2020:2021)
its times 10 because of 10 years. (In the example only 2) 10 of the columns just specify year in all columns (I made them as 1) 180 define values for 18 categories for each year 31 columns define values that are identical for all those years.
Any solutions?