[R] reshape

Jun Yan jyan at stat.wisc.edu
Mon Oct 14 06:14:28 CEST 2002


juli, assuming your "gen" is the identification variable for clusters, the
following is an illustration.

Jun

> dat <- data.frame(gen=rep(1:5, rep(2,5)), maxh=rnorm(10),
resp=rep(c("y", "n"), 5))
> dat
   gen        maxh resp
1    1 -1.53878091    y
2    1 -0.71171477    n
3    2  0.93065758    y
4    2 -1.65696752    n
5    3 -0.09981616    y
6    3  0.08186816    n
7    4  0.11563345    y
8    4  0.04991344    n
9    5  0.67936629    y
10   5 -1.29308856    n
> dat.w <- reshape(dat, idvar="gen", timevar="resp", v.names="maxh",
direction="wide")
> dat.w
  gen      maxh.n      maxh.y
1   1 -0.71171477 -1.53878091
3   2 -1.65696752  0.93065758
5   3  0.08186816 -0.09981616
7   4  0.04991344  0.11563345
9   5 -1.29308856  0.67936629
>

On Sat, 12 Oct 2002, juli g. pausas wrote:

> I'd like to convert a data.frame from (long format):
> gen   maxh  resp
> 1         12.3   y
> .           .       .
> .           .       .
> where resp is a factor with levels "y" and "n"
> to the wide format:
> 
> gen  maxh.y  maxh.n
> .           .       .
> .           .       .
> 
> I've done it as follows:
> 
> maxh.y <- split(maxh, resp)$y
> gy <- split(gen, resp)$y
> yes <- data.frame(hr, gen=gy)
> 
> maxh.n <- split(maxh, resp)$n
> gn <- split(gen, resp)$n
> no <- data.frame(hn, gen=gn)
> 
> wide <- merge(yes, no, by= "gen") # gen maxh.y maxh.n
> 
> This worked well, but would it be possible to do it with reshape? I've
> tried but I didn't succeed.
> Thanks
> 
> Juli
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list