[R] Data Frame Transpose

Uwe Ligges ligges at statistik.tu-dortmund.de
Wed Jan 6 10:35:58 CET 2010



On 06.01.2010 03:14, Noli Sicad wrote:
> Hi John
>
> Thanks for your reply. I think I was posting properly the problem.
>
> Here are the error, R script and console errors below.
>
> Thanks. Noli
>
> ~~~
> The error:
> ~~~~~~~~~~
> Error in data.frame(CROP_ID = x[1, 1], CROPTYPE = x[1, 2], name =
> colnames(x)[4:5],  :
>   subscript out of bounds


Probably x does not have at least 5 columns ...
Since we do not have the full data, we cannot see what exactly happens.

Best,
Uwe Ligges


> ~~~~~~~~~~~
>
> I have a dynamic subscripts for the Period, as result of linear
> programming (LP) model run. How I generalise this line. Right now it
> has 3 index only.
>
> x01=y[,1], x02=y[,2], x03=y[,3])
>
> This is sample the data.
>
> PERIOD
> 1
> 1
> 1
> 1
> 2
> 2
> 2
> 2
> 3
> 3
> 3
> 4
> 4
> 5
> 5
> 5
> 5
> 5
> 6
> 6
> 6
> 6
> 6
> 6
> 7
> 8
> 9
> 10
> 10
> 10
> 10
>
>
>
> R  script:
> ~~~~~~~~~~~~
> harvest.dat<- read.dbf('C:\\Down2\\R_forestmgt\\Carbon\\forest_cut_m.dbf')
>
> names(harvest.dat) = c("CROP_ID", "CROPTYPE", "PERIOD","CUT_AGE", "AREA_CUT")
>
> # Transpose 5 columns
>
> fn<- function(x) {
>   y<- t(x[,4:5])
>   data.frame( CROP_ID=x[1,1], CROPTYPE=x[1,2], name=colnames(x)[4:5],
> x01=y[,1], x02=y[,2], x03=y[,3])
>   }
>
> harvest.dat<- do.call( "rbind",
> lapply(split(harvest.dat,list(harvest.dat$CROP_ID,harvest.dat$CROPTYPE)),fn)
> )
>
> write.csv(harvest.dat, "forest_cut3.csv")
>   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Scite console with r package
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> Rscript --vanilla --slave "C:\Down2\R_forestmgt\Carbon\ForestCarbon_1_F_Clean7_transpose.R"
> [1] "C:/Down2/R_forestmgt/Carbon"
> Loading required package: foreign
> Loading required package: sp
> Loading required package: methods
> Loading required package: lattice
> Warning messages:
> 1: package 'maptools' was built under R version 2.10.1
> 2: package 'foreign' was built under R version 2.10.1
> 3: package 'sp' was built under R version 2.10.1
> Error in data.frame(CROP_ID = x[1, 1], CROPTYPE = x[1, 2], name =
> colnames(x)[4:5],  :
>   subscript out of bounds
> Calls: do.call ->  lapply ->  FUN ->  data.frame
> Execution halted
>> Exit code: 1    Time: 2.128
> ~~~~~~~~~~~~~~~~~~~~~~~~~
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list