[R] tapply

rohan sadler rsadler at agric.uwa.edu.au
Thu Dec 5 03:13:03 CET 2002


Hi Bryn,

You just need to use the na.rm=T option (look under help for the mean 
function)
e.g.
 > x<-c(1,4,6,NA,10,18)    # data
 > mean(x)
[1] NA
 > mean(x,na.rm=T)
[1] 7.8

 > y<-c(1,1,1,2,2,2)        # factor variable
 > tapply(x,y,mean)
       1        2
3.666667       NA
 > tapply(x,y,mean,na.rm=T)
        1         2
 3.666667 14.000000

Rohan


rbf21 wrote:

>I'm having trouble with tapply, it seems to give me NA for the mean of a set 
>like {2, 3, 4, NA}, can I get it to ignore the NA's and give me a mean like 
>say, three? Am I just doing something stupid?
>
>Any help would be apprecitated, Also are there PDF versions of any of the R 
>stats books around that I might be able to get my hands on?
>
>Bryn
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>http://www.stat.math.ethz.ch/mailman/listinfo/r-help
>

-- 
Ecosystems Research Group (ERGO)
School of Plant Biology (Botany), Faculty of Natural & Agricultural Sciences,
The University of Western Australia, 35 Stirling Highway, Crawley  WA  6009, Australia

Ph:  +61 8 9380 7914
Fax: +61 8 9380 7925
email: rsadler at agric.uwa.edu.au
ERGO's web site:<http://www.botany.uwa.edu.au/ergo>




More information about the R-help mailing list