# [R] Potential problem with tapply

Philippe Lambert phlamber at luc.ac.be
Fri Feb 19 16:16:39 CET 1999

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

> # Works fine
> tapply(x,fac,quantile,na.rm=T)
\$"1"
0%  25%  50%  75% 100%
2    8   11   12   12

\$"2"
0%   25%   50%   75%  100%
3.00  3.75  5.50  8.00 11.00

\$"3"
0%   25%   50%   75%  100%
1.00  1.75  4.50  9.75 18.00

\$"4"
0%  25%  50%  75% 100%
5.0  5.5  6.0  6.5  7.0

> # Problem
> tapply(x,fac,quantile,p=.6,na.rm=T)
Warning: NAs introduced by coercion
1  2  3  4
NA NA NA NA

> tapply(x,fac,quantile,na.rm=T,p=.6)
Warning: NAs introduced by coercion
1  2  3  4
NA NA NA NA

> # But lapply works...
> lapply(split(x,fac),quantile,p=.6,na.rm=T)
\$"1"
60%
11.6

\$"2"
60%
6.4

\$"3"
60%
6

\$"4"
60%
6.2

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

```