[英]How to pass na.rm=TRUE to sapply when calculating median?
I have created a dataframe "killers" with 3 variables. 我创建了一个包含3个变量的数据帧“杀手”。 The data are numeric though there exist NA
values throughout. 虽然整个存在NA
值,但数据是数字的。
My goal is to calculate the mean on each of the 3 variables. 我的目标是计算3个变量中每个变量的均值。
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. 我知道na.rm
参数是一种忽略NA
值的方法。 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. 由于在R返回的内容中存在na.rm = FALSE
,因此可以假设有一种方法可以在上面的代码行中将其设置为TRUE
。 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. 我不确定我是否接近或者是否会涉及嵌套功能,因为其他类似的(尽管在这个实例中我最终没有帮助)我会在SO主题上发布帖子。 eg How to pass na.rm as argument to tapply? 例如, 如何将na.rm作为参数传递给tapply? , Ignore NA's in sapply function , 忽略NA的功能
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. 当然,我可以计算用于创建杀手的每个向量的均值,但是如果我问的是可能的那么那就更好了。
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.