Skip to content

Data Frame Transpose

4 messages · John Kane, Noli Sicad, Uwe Ligges

#
Hi,

forests <- read.csv("C:\\Down2\\R_forestmgt\\forest_cut-Age.csv")

m <- forests

fn <- function(x) {
  y <- t(x[,2])
  data.frame( Croptype=x[1,1], Period =x[1,2], name=colnames(x)[2],
x01=y[,1])x01=y[,1], x02=y[,2], x03=y[,3] } <---------------Problem
here

m <- do.call( "rbind", lapply(split(m,list(m$Period,m$Croptype)),fn) )

m <- m[order(m$Period,m$Croptype),]


I think I having a problem in here: x01=y[,1])x01=y[,1], x02=y[,2],
x03=y[,3]. how to address with my data. I have variable Period.

based on this http://www.mail-archive.com/r-help at stat.math.ethz.ch/msg09264.html

P_ID Croptype  Period  Ini_Age  Area_Cut
83      SORI    1       31      528.2465512
84      SORI    1       32      74.55179899
85      SORI    1       33      72.45778618
86      SORI    1       34      139.5272947
82      SORI    2       28      1.711642933
83      SORI    2       29      2.500000071
84      SORI    2       30      432.5139327
93      ORM    2       35      316.8422545
62      OTRM    3       30      64.60526438
82      SORI    3       27      26.93674606
3       SORM    3       35      223.3658345
82      SORI    4       26      2.500000071
4       SORM    4       34      1008.643
5       OTRI    5       25      32.42603214
5       OTRM    5       29      65.9031344
5       SORM    5       32      223.1489321
5       SORM    5       33      72.59203041
5       SORM    5       35      222.8402746
6       OTRI    6       22      2.499999851
6       OTRI    6       23      3.374626509
6       OTRI    6       24      96.13462257
6       OTRM    6       26      830.7463641
6       OTRM    6       27      731.6228643
6       OTRM    6       28      16.3519762
7       OTRM    7       26      1636.5693
8       OTRM    8       26      553.0050146
9       OTRM    9       26      894.414033
10      OTRM    10      24      38.72597099
10      OTRM    10      25      308.6452707
10      OTRM    10      26      786.1761969
10      SORM    10      31      235.8360136

To this.

P_ID Croptype P1        P2        P3        P4       P5        P6    P7
  P8        P9        P10
83      SORI    31
84      SORI    32
85      SORI    33
86      SORI    34
82      SORI            28
83      SORI            29
84      SORI            30
93      SORM            35
62      OTRM                    30
82      SORI                    27
3       SORM                    35
82      SORI                            26
4       SORM                            34
5       OTRI                                    25
5       OTRM                                    29
5       SORM                                    32
5       SORM                                    33
5       SORM                                    35
6       OTRI                                            22
6       OTRI                                            23
6       OTRI                                            24
6       OTRM                                            26
6       OTRM                                            27
6       OTRM                                            28
7       OTRM                                                    26
8       OTRM                                                            26
9       OTRM

Thanks in advance. Noli
#
Well, if nothing else, you have missing comma.  :)

x01=y[,1]), x01=y[,1], x02=y[,2], x03=y[,3]
          --
__________________________________________________________________
Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now
http://ca.toolbar.yahoo.com.
#
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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
~~~~~~~~~~~~~~~~~~~~~~~~~
#
On 06.01.2010 03:14, Noli Sicad wrote:
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