[R] Comparing dcast and reshape

Nutter, Benjamin NutterB at ccf.org
Wed Oct 17 23:33:37 CEST 2012


Thanks for the prompt reply, Dr. Winsemius.

reshape(FullLong)  

Will return the original data frame, but that wasn't quite what I wanted.  I wanted to take it to a wider form than the original.

But I just answered my own question (typically, _after_ asking the world at large).

FullWide <- reshape(Raw,
        v.names=c("base.score", "score.90d"),
        timevar="instrument",
        idvar=c("site", "id"),
        direction="wide")

gives me what I was looking for.  I suspect there isn't a way to go directly from FullLong to FullWide, but if someone can prove me wrong, I'd love to see how.

  Benjamin Nutter |  Biostatistician     |  Quantitative Health Sciences
  Cleveland Clinic    |  9500 Euclid Ave.  |  Cleveland, OH 44195  | (216) 445-1365



-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net] 
Sent: Wednesday, October 17, 2012 5:18 PM
To: Nutter, Benjamin
Cc: r-help at r-project.org
Subject: Re: [R] Comparing dcast and reshape


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


===================================


 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:18}}




More information about the R-help mailing list