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?
Thanks for taking the time to educate me.
Benjamin Nutter | Biostatistician | Quantitative Health Sciences
Cleveland Clinic | 9500 Euclid Ave. | Cleveland, OH 44195 |
(216) 445-1365
==================================
Please consider the environment before printing this e-mail
Cleveland Clinic is ranked one of the top hospitals in America by U.S.News &
World Report (2012).
Visit us online at http://www.clevelandclinic.org for a complete listing of our
services, staff and locations.
Confidentiality Note: This message is intended for use ...{{dropped:15}}
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