1

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!

DM1
  • 11
  • 2
  • Actual data sample of current and desired results may help to illustrate your needs and help us reproduce your issue. We do have your data in front of us! – Parfait Feb 05 '16 at 04:59
  • Thanks for your reply; I'm guessing the datasets are a bit too big to paste directly into the question, but the input dataset 'french_fries' is included in the 'reshape2' package, so the code should be runnable. I added an extra line to illustrate the desired results (data.frame 'd'). – DM1 Feb 11 '16 at 17:11

0 Answers0