[R] tapply and empty subsets

Giovanni Petris GPetris at uark.edu
Tue May 14 18:59:15 CEST 2002

I am using tapply to compute means by group, as in

> tapply(y, z, mean)
       1        2        3 
21.00757 20.50031       NA 

where y is a vector and z a factor with levels 1:3.
I would like to get a zero instead of the NA I get when a subset is
empty. The problem is that FUN is not applied to empty cells of the
ragged array. (Of course I am willing to define a function my.mean
that returns 0 for empty vectors)

Does anybody have a suggestion for an easy/fast/elegant workaround? 

Thanks in advance,


[                                                  ]
[ Giovanni Petris                 GPetris at uark.edu ]
[ Department of Mathematical Sciences              ]
[ University of Arkansas - Fayetteville, AR 72701  ]
[ Ph: (479) 575-6324, 575-8630 (fax)               ]
[ http://definetti.uark.edu/~gpetris/              ]

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