[R] Data Frame Transpose
Noli Sicad
nsicad at gmail.com
Wed Jan 6 03:14:18 CET 2010
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
~~~~~~~~~~~
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
~~~~~~~~~~~~~~~~~~~~~~~~~
More information about the R-help
mailing list