1

Newb here - unable to figure out some basic syntax and given the common-ness of the words google isnt helping....

mtcars$testcol = 'testing'
imgood = sapply(mtcars, IQR, na.rm=T) #works ok... helpme =
sapply(mtcars, quantile c(.3,.9, .95, na.rm=TRUE)  #don't know how to

Then what I would like to do is just

mtcars$mynewcolumn = imgood....etc

pass in parameters with sapply/apply...

Also I have been looking at summarise_each, summarise_all with dplyr:: Is there a way this would work here?

scbears88
  • 67
  • 1
  • 6
  • 1
    How about ```sapply(mtcars, function(x) { quantile(x,c(.3,.9, .95, na.rm=TRUE)) })```? – Abdou Jul 25 '16 at 20:15
  • This is a typo. You missed a comma `sapply(mtcars, quantile, c(.3,.9, .95, na.rm=TRUE))` – Rich Scriven Jul 25 '16 at 20:33
  • This works until I add in a non-numeric column like I did above. I tried something like ----sapply(mtcars, function(x) {ifelse(is.numeric(x), quantile(x,c(.3,.9, na.rm=TRUE)),'xxx') }) ---- but then I don't get output for multipel values – scbears88 Jul 25 '16 at 21:01
  • 1
    Then only run it on the numeric columns; it doesn't make sense to compute IQR for non-numeric columns, anyway. Or are we missing something? It may help to show us what you're starting with and what you want to end with, and then after that tell us what you've tried. – Aaron left Stack Overflow Jul 25 '16 at 21:22
  • @Aaron is right. There is no point in getting a percentile of a character vector. Hence, the name of the function ***quantile***. – Abdou Jul 25 '16 at 22:09
  • Yes, I was trying to just having to avoid doing all the splitting/prep work where it would just pass over non-numeric data. So I would just see NA if it was a factor or character ro something – scbears88 Jul 25 '16 at 22:42
  • But where you're really losing us is what you want to do with the result. Somehow you want to add a new column with the results, but it won't have the right dimensions, even if everything is numeric. – Aaron left Stack Overflow Jul 26 '16 at 04:52

1 Answers1

1

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.

IRTFM
  • 258,963
  • 21
  • 364
  • 487