I would like to reshape a dataframe that looks like this:
x y z Date
x1 y1 z1 1
x2 y2 z2 2
x3 y3 z3 3
into something like this:
var val Date
x x1 1
x x2 2
x x3 3
y y1 1
y y2 2
y y3 3
z z1 1
z z2 2
z z3 3
I have tried this, but I lose the date variable this way:
cols1<-colnames(y[-ncol(y)]) ## to drop the Date
df_new <- stack(df, select=c(cols1))
Is there a simple way to do this? I searched through the forum (which has a ton of reshaping questions obviously) but I could not find one that tried to do what I need to do.
This forum question for instance looks into a different problem. The original dataframe is a in different format, all the dates are individual columns. My date is one column only: Reshaping data.frame from wide to long format