[R] reshape: duplicate rows to multiple cols

Jun Yan jyan at stat.wisc.edu
Sat Oct 26 06:10:52 CEST 2002


What you need is to specifying arguments idvar, timevar, drop, and v.names
like the following:

> klam
          stream lulc          x sumlength    pct.lgth
201 1.223030e+12   23 0.00274154      4.73  0.05796068
202 1.223030e+12   41 0.75009917      4.73 15.85833341
203 1.223030e+12   42 2.65000000      4.73 56.02536998
204 1.223030e+12   43 0.11715929      4.73  2.47694058
205 1.223030e+12   51 0.71000000      4.73 15.01057082
206 1.223030e+12   71 0.50000000      4.73 10.57082452
> klam.w <- reshape(klam, idvar="stream", timevar="lulc", drop="x",
v.names="pct.lgth", direction="wide")
> klam.w
          stream sumlength pct.lgth.23 pct.lgth.41 pct.lgth.42 pct.lgth.43
pct.lgth.51 pct.lgth.71
201 1.223030e+12      4.73  0.05796068    15.85833    56.02537    2.476941
15.01057    10.57082


Jun Yan

Department of Statistics          Office: CSSC 4252
university of Wisconsin-Madison   Tel: (608)262-7478 
1210 W. Dayton St.                Email: jyan at stat.wisc.edu
Madison, WI 53706                 URL: http://www.stat.wisc.edu/~jyan

On Fri, 25 Oct 2002, Robert Schick wrote:

> I have a dataframe that I'm trying to reshape, and need advice. My data:
> 
> > klam.merge[200:225,]
>            stream lulc          x sumlength    pct.lgth
> 200 1223030419685   92 0.25000000      9.89  2.52780586
> 201 1223030419686   23 0.00274154      4.73  0.05796068
> 202 1223030419686   41 0.75009917      4.73 15.85833341
> 203 1223030419686   42 2.65000000      4.73 56.02536998
> 204 1223030419686   43 0.11715929      4.73  2.47694058
> 205 1223030419686   51 0.71000000      4.73 15.01057082
> 206 1223030419686   71 0.50000000      4.73 10.57082452
> 
> I want the reshape to have the pct.lgth value put into its corresponding
> lulc col, e.g.
> stream		sumlength	lulc.23		lulc.41		lulc.42		lulc.51 ...
> 1223030419686	4.73		0.05796068	15.85833341	56.02536998     15.01057082
> 
> I've tried:
> 
> >klam.wide <- reshape(klam.merge, varying=list(levels(klam.merge$lulc)), + timevar="stream",drop="x",direction="wide")
> 
> which yields
> 
> Error in "[<-.data.frame"(*tmp*, , varying[, i], value =
> thistime[match(rval[,  : 
>         subscript out of bounds
> In addition: There were 20 warnings (use warnings() to see them)
> >
> 
> I've also tried above syntax with times=list(levels(klam.merge$lulc))
> but I get the same error.
> 
> Thoughts on what I'm missing?
> 
> -- 
> Rob Schick
> Research Associate
> NOAA Fisheries
> Santa Cruz Lab
> 110 Shaffer Road
> Santa Cruz, CA 95060
> Phone: 831.420.3960
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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