[R] Potential problem with tapply

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Feb 19 17:02:21 CET 1999


Philippe Lambert <phlamber at luc.ac.be> writes:

> Is the following behaviour of tapply not disappointing?
> Problem with tapply occurs when dealing with na.rm when an
> argument additional to na.rm is sent to the applied function (here
> quantile). 
> 
> Any comment?
> Thank you,
>  Philippe Lambert
> 
> > x <- c(12,10,12,2,4,11,3,7,2,1,18,7,NA,NA,7,5)
> > fac <- gl(4,4,16)
....
> > # Problem
> > tapply(x,fac,quantile,p=.6,na.rm=T)
> Warning: NAs introduced by coercion
>  1  2  3  4 
> NA NA NA NA 

Ick! This comes from the fact that the result of quantile is named, so
that unlist()'ing it gives names "1.60%", "2.60%", and as.numeric on
this gives.... The fix is to move the line 

index <- as.numeric(names(ans))

up before the if()-else construction.

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