包含NA的字段上的范围 [英] Range on a field containing NAs
问题描述
我正在使用一个数据集,其中csv文件上的第11列包含数字数据.它也包含一些NA值.这是对象的str:
I'm using a data set where the 11th column on a csv file has numeric data. It contains some NA values too. Here is the str of the object:
str(dataheart)
num [1:4706] 14.3 18.5 18.1 NA NA NA 17.7 18 15.9 NA ...
所以,作为R的新学生,我曾期望range(dataheart)
的结果是最小值和最大值.通过查看包含数据的CSV文件,我知道最小值和最大值分别为10.1和21.9.
So, as a new student of R, I had expected the result of range(dataheart)
to be the min and max values.From looking at the CSV file with data, I know that the min and max are 10.1 and 21.9.
但是上面的返回向量
[1] NA NA
我对此功能的理解不正确吗?
Is my understanding of this function incorrect?
推荐答案
您需要
range(x,na.rm=TRUE)
请参见?range
要获得额外的荣誉,下面是使用na.rm
的base
和stats
软件包中的功能列表:
For extra credit, here's a list of the functions in the base
and stats
packages that use na.rm
:
uses_na_rm <- function(x) is.function(fx <- get(x)) &&
"na.rm" %in% names(formals(fx))
basevals <- ls(pos="package:base")
basevals[sapply(basevals,uses_na_rm)]
## [1] "colMeans" "colSums"
## [3] "is.unsorted" "mean.default"
## [5] "pmax" "pmax.int"
## [7] "pmin" "pmin.int"
## [9] "range.default" "rowMeans"
## [11] "rowsum.data.frame" "rowsum.default"
## [13] "rowSums" "Summary.data.frame"
## [15] "Summary.Date" "Summary.difftime"
## [17] "Summary.factor" "Summary.numeric_version"
## [19] "Summary.ordered" "Summary.POSIXct"
## [21] "Summary.POSIXlt"
statvals <- ls(pos="package:stats")
statvals[sapply(statvals,uses_na_rm)]
## [1] "density.default" "fivenum" "heatmap" "IQR"
## [5] "mad" "median" "median.default" "medpolish"
## [9] "quantile.default" "sd" "var"
为了进一步考虑R中的哪些函数处理NA
以及如何处理,人们可以使用na.action
参数(lm
和它的朋友)对函数进行类似的搜索.
For further consideration of which functions in R deal with NA
s and how, one could do an analogous search for functions with an na.action
argument (lm
and friends).
这篇关于包含NA的字段上的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!