I am working with a dataset that comes with lme4, and I'm trying to learn how to use reshape2 to convert it from long to wide [full code at the end of the message].
library(lme4) data("VerbAgg")
The data set has 9 variables; "Anger", "Sex" and "id" are not dependent on the "element", but "resp", 'btype', 'situ', 'mode' and 'r2' do.
I was able to successfully convert the data set from long to wide using the reshape () function:
wide <- reshape(VerbAgg, timevar=c("item"), idvar=c("id", 'Gender', 'Anger'), dir="wide")
Which gives 316 observations on 123 variables and is apparently correctly transformed. However, I had no success using reshape / reshape2 to play back a wide data frame.
wide2 <- recast(VerbAgg, id + Gender + Anger ~ item + variable) Using Gender, item, resp, id, btype, situ, mode, r2 as id variables Error: Casting formula contains variables not found in molten data: Anger
I cannot 100% understand how recast defines identification variables, but I am very confused why it does not see "Anger". Similarly
wide3 <- recast(VerbAgg, id + Gender + Anger ~ item + variable, id.var = c("id", "Gender", "Anger")) Error: Casting formula contains variables not found in molten data: item
Can anyone see what I'm doing wrong? I would like to get a better understanding of melt / casting!
Full code:
#