I want to convert, with as little code as possible, the following DF so that the date variable remains as a column, but the factor levels each become variable names and the data of each level becomes the data in each of these respective variables. For example, the variable red should contain the values 50, 45 and 55. I have considered filtering, transposing and melting and casting, but I cannot figure out a way.
Please help! Thank you!
df <- data.frame(factor1 = factor(x=rep(1:3, 3), levels=1:3, labels=c("red", "blue", "green")),
data1 = c(50, 35, 15, 45, 40, 25, 55, 35, 10),
date1 = rep(c(2000, 2004, 2008), each=3))