How to pass na.rm=TRUE to sapply when calculating median?

Doug Fir picture Doug Fir · Jan 22, 2013 · Viewed 22.9k times · Source

I have created a dataframe "killers" with 3 variables. The data are numeric though there exist NA values throughout.

My goal is to calculate the mean on each of the 3 variables.

sapply(killers, function(x) median)

This returns:

$heartattack
function (x, na.rm = FALSE) 
UseMethod("median")
<bytecode: 0x103748108>
<environment: namespace:stats>

I know that the na.rm argument is a means to ignore NA values. Since na.rm = FALSE exists in what was returned by R, one presumes that there is a way to set this to TRUE within the line of code above. I tried a few variations:

sapply(killers, na.rm=TRUE function(x) median)
sapply(killers, function(x) median, na.rm=TRUE)
sapply(killers, function(x) median(na.rm=TRUE))

I'm not sure if I'm close or if this is going to involve nesting functions, as per other similar (though ultimately not helpful in this instance that I can see) posts on the topic on SO. e.g. How to pass na.rm as argument to tapply?, Ignore NA's in sapply function

Of course, I could just calculate the mean on each vector that was used to create killers, but surely if what I'm asking is possible then that is better.

Answer

Jilber Urbina picture Jilber Urbina · Jan 22, 2013

Just do:

sapply(killers, median, na.rm = TRUE)

An alternative would be (based on your code)

sapply(killers, function(x) median(x, na.rm=TRUE))