[R] tapply and empty subsets
Giovanni Petris
GPetris at uark.edu
Tue May 14 21:13:47 CEST 2002
I guess for my purpose the following will do.
> x <- tapply(y, z, mean)
> x[is.na(x)] <- 0
Thanks,
Giovanni
> Date: Tue, 14 May 2002 11:59:15 -0500 (CDT)
> X-Authentication-Warning: definetti.uark.edu: gpetris set sender to gpetris at definetti.uark.edu using -f
> From: Giovanni Petris <GPetris at uark.edu>
> Reply-to: Giovanni Petris <GPetris at uark.edu>
> Sender: owner-r-help at stat.math.ethz.ch
> Precedence: SfS-bulk
> Content-Type: text
> Content-Length: 1298
>
>
> 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
>
> --
>
> __________________________________________________
> [ ]
> [ 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
--
__________________________________________________
[ ]
[ 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