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