up vote 10 down vote favorite
1

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.

share|improve this question
3  
?sapply says "...: optional arguments to 'FUN'." and there are examples of how to pass additional arguments to functions in the Examples section. – Joshua Ulrich Jan 22 '13 at 16:38
    
@JoshuaUlrich not meant to be that offensive. – user1317221_G Jan 22 '13 at 16:41
2  
You asked sapply to return the function median and that's exactly what it did. Functions are objects. (What you did not do was tell it to return median(x).) – 42- Jan 22 '13 at 17:55

1 Answer 1

active oldest votes
up vote 27 down vote accepted

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))
share|improve this answer

Not the answer you're looking for? Browse other questions tagged r na sapply na.rm or ask your own question.