问题
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 ...
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.
But the above returns a vector
[1] NA NA
Is my understanding of this function incorrect?
回答1:
You need
range(x,na.rm=TRUE)
see ?range
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"
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).
来源:https://stackoverflow.com/questions/14426033/range-on-a-field-containing-nas