[R] Tranpose and Aggregate Data - now Reshape - cast

Noli Sicad nsicad at gmail.com
Tue Jan 5 11:03:03 CET 2010


library(reshape)

names(harvest.dat) = c("CROP_ID", "CROPTYPE", "PERIOD","CUT_AGE")

harvest <-cast(harvest.dat, CROP_ID + CROPTYPE ~ PERIOD)

It seems that I am getting the frequencies instead of the individual values.

Output
~~~~~~~~~
   CROP_ID CROPTYPE 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1         1      OTO 2 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0
2         1     SORI 4 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0
3         2      OTO 0 6 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0
4         2     SORI 0 3 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0
5         2     SORM 0 1 0 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0
6         3      OTO 0 0 2 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0
7         3     OTRM 0 0 1 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0
8         3     SORI 0 0 1 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0
9         3     SORM 0 0 1 0 0 0 0 0 0  0  0  0  0  0  0  0  0  0  0  0  0  0
~~~~~~~~~


Desired table

CROP_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

How do I do this properly.

Thanks in advance. Noli



More information about the R-help mailing list