[R] Pivoting a data.frame

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Nov 10 13:37:51 CET 1998


Athula Herath <Athula.Herath at ogs.co.uk> writes:

> Hi,
>  
> I would like tranform a data.frame which has a structure similar to the:
>  
> Col1    Col2    Col3
> B1      1       0.3
> B1      2       0.4
> B1      3       0.3
> B2      1       0.2
> B2      2       0.2
> B2      5       0.4
> B2      6       0.1
> B3      1       0.2
> B3      2       0.1
> B3      5       0.2
>  
> [Data Frame 1]
>  
> Into the following structure (pivoting or transpose is
> being used sometimes to describe this, I believe).
>   
>         B1      B2      B3
> 1       0.3     0.2     0.2
> 2       0.4     0.2     0.1
> 3       0.3     NA      NA
> 5       NA      0.4     0.2
> 6       NA      0.1     NA
>  
> [Data Frame 22]
>  
> As you see above, the columns of Table 1 is rearrange to
> categorise the third column (in terms of Col1 and Col2).
>  
> I wonder whether anybody here could help me to code this
> in S-Plus or R, or point me on the direction how to do
> this.

dd<-read.table('herath.dat',head=T)
attach(dd)
tapply(Col3,list(Col2,Col1),mean) #or median or whatever...

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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