Unfortunately quantile
was not designed to return a vector of NA's or a single NA. To get this to happen you could write an enclosing function that would act the way you were hoping:
my_quantile <- function(x, ...) if ( is.numeric(x) ) {quantile(x,...)} else {
z <- list(...)[[1]]; rep(NA,length(z))}
> sapply(mtcars, my_quantile, c(.3,.9, .95), na.rm=TRUE)
mpg cyl disp hp drat wt qsec vs am gear carb testcol
30% 15.98 4 142.06 106.20 3.1500 2.77300 17.0200 0 0 3 2.0 NA
90% 30.09 8 396.00 243.50 4.2090 4.04750 19.9900 1 1 5 4.0 NA
95% 31.30 8 449.00 253.55 4.3145 5.29275 20.1045 1 1 5 4.9 NA
It's probably not the first function you would write, since it requires extracting the second argument passed to quantile to repeat NA's the number of times to match the other quantiles which in turn allows sapply
to retrun a matrix rather than a list. It's also going to be a bit fragile, since you are not naming your arguments. If probs
had been named then it might not have been first, so it might have been better to check to see whether match.args
could find a probs
argument, and then, if that failed, to use the first argument in the ...
-argument-list.