On Oct 17, 2012, at 2:09 PM, Nutter, Benjamin wrote:
I'm in the middle of my own little intellectual exercise comparing
dcast() and reshape() and have successfully stumped myself. I want to
melt() a data frame, then dcast() it into a new form. After doing
so, I
want to duplicate the process using reshape().
So far, I can do the melt and cast
require(reshape2)
Raw <- data.frame(site = c(1, 1, 1, 1, 2, 2, 2, 2),
id = c(1, 1, 2, 2, 1, 1, 2, 2),
instrument = rep(c("beck", "phq"), 4),
base.score = c(27, 13, 31, 11, 22, 10, 41, 17),
score.90d = c(20, 11, 27, 12, 24, 8, 34, 15))
Full.Melt <- melt(Raw, id.vars=c("site", "id", "instrument"),
measure.vars=c("base.score", "score.90d"))
FullCast <- dcast(Full.Melt, site + id ~ instrument + variable,
value.var="value")
FullCast
site id beck_base.score beck_score.90d phq_base.score phq_score.90d
1 1 1 27 20 13 11
2 1 2 31 27 11 12
3 2 1 22 24 10 8
4 2 2 41 34 17 15
I can also replicate the melt using reshape, but I can't reshape it
into
the same wide format.
FullLong <- reshape(Raw,
varying=list(score=c("base.score", "score.90d")),
idvar=c("site", "id", "instrument"),
direction="long")
Any pointers on how to get FullLong into the same wide format as
FullCast?
The reshape function will "recognize" that the object was created as a
wide->long reshaping and if you just use this code, you will get back
the original:
reshape(FullLong)
--
David Winsemius, MD
Alameda, CA, USA
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.