[R] reshape: duplicate rows to multiple cols

Robert Schick Robert.Schick at noaa.gov
Fri Oct 25 23:45:12 CEST 2002


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list