[R] Re: problem with tapply

Heberto Ghezzo heberto.ghezzo at mcgill.ca
Wed Sep 25 22:33:36 CEST 2002


Hello, I defined a function funy to get means(sd)n for groups in a dataframe

  funy<-function(x) list(mean(x,na.rm=T),sd(x,na.rm=T),table(is.na(x))[1])
   aa<-tapply(y,list(g1,g2,g3),funy)

typing
aa
gives
, , G.Baie

     Admin  CBNE   Coulee Electr ExCuv Fluo ExpAnt
Non  List,3 List,3 List,3 List,3 NULL  NULL NULL
Ex   List,3 List,3 List,3 List,3 NULL  NULL NULL
Curr List,3 List,3 List,3 List,3 NULL  NULL NULL

, , Later

     Admin  CBNE   Coulee Electr ExCuv  Fluo   ExpAnt
Non  List,3 List,3 List,3 List,3 List,3 List,3 List,3
Ex   List,3 List,3 List,3 List,3 List,3 List,3 List,3
Curr List,3 List,3 List,3 List,3 List,3 NULL   List,3

, , Shawi

     Admin  CBNE   Coulee Electr ExCuv  Fluo ExpAnt
Non  List,3 List,3 List,3 List,3 List,3 NULL List,3
Ex   List,3 List,3 List,3 List,3 List,3 NULL List,3
Curr List,3 List,3 List,3 List,3 List,3 NULL List,3

but I want the componenets of the lists, so I do
   bb<-matrix(unlist(aa),nrow=3)
now typing
bb
gives me
          [,1]       [,2]      [,3]      [,4]      [,5]     [,6]     [,7]
[1,] 104.10811 103.363636 101.90909 102.02439 104.10000 97.69149 98.56522
[2,]  11.34995   9.564545  13.52372  14.40420  11.88250 13.63820 12.12370
[3,]  37.00000  22.000000  11.00000  82.00000  50.00000 94.00000 23.00000
         [,8]     [,9]    [,10]     [,11]    [,12]     [,13]     [,14]    [,15]
[1,] 98.31250 99.24138 99.96250 100.49020 97.27586 102.82759 107.33333 97.15385
[2,] 15.36975  9.25687 11.94761  13.93897 12.77870  12.53301  10.03026 14.12354
[3,] 16.00000 29.00000 80.00000  51.00000 87.00000  29.00000  12.00000 13.00000
         [,16]    [,17]    [,18]     [,19]     [,20]    [,21]     [,22]
[1,] 101.00000 97.85000 94.00000 96.333333 102.21429 95.50000 100.18033
[2,]  11.48291 14.70866 18.64806  9.232448  13.31961 15.88325  12.38616
[3,]  15.00000 20.00000 17.00000 15.000000  14.00000 10.00000  61.00000

i.e. the NULL lists are bypassed and I lose all relationship with the levels of the
by variables
Is there a way to have lists of 3 NA's in the place of the NULL lists
As always many thanks for any help.

--
R. Heberto Ghezzo Ph.D.
Meakins-Christie Labs
McGill University
Montreal  -  Canada


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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