I'm writing to ask for help understanding a behavior of reshape2::dcast that is confusing me. When I run the following four lines (the example dataset 'french_fries' comes with the 'reshape2' package), I get three data.frames with different lengths, whereas I expect them to be identical except for permuted rows and columns:
library('reshape2')
ff_d <- melt(french_fries, id=1:4, na.rm=TRUE) # "french_fries" is included in the 'reshape2' package
a = dcast(ff_d, treatment + subject ~ variable, mean, margins=T) # I think these data.frames are too long to paste directly into the question
b = dcast(ff_d, subject + treatment ~ variable, mean, margins=T)
d = merge(a, b, all = T) # I was expecting 'd' and 'a' (and 'b') to be identical.
Are the variables on the left side of the formula are being treated as nested, so that we only get margins of the second variable within levels of the first? I'd be grateful for any insights, and particularly for explanations of how to get all three types of margins using one function call, e.g. treament=all by subject, subject=all by treatment, and both=all.
Many thanks!