I want a vector with integers and NAs to always be returned as class integer.
Even if a vector is all NA (of class integer)
My criticism of min/max is that they don't return the same class for a given class of input.
Here is my best shot as a workaround - to be used in the situation where I know I want to use na.rm = TRUE
minNA = function(x) {
returnval <- min(x, na.rm = TRUE)
if (is.finite(returnval)) {
returnval
} else {
returnval <- as(NA, class(x))
}
}
inputdata <- rep(as.integer(NA),5)
print(inputdata)
print(class(inputdata))
#minvalue <- minNA(inputdata, na.rm = TRUE)
minvalue <- minNA(inputdata)
print(minvalue)
print(class(minvalue))
1] NA NA NA NA NA
[1] "integer"
[1] NA
[1] "integer"
Warning message:
In min(x, na.rm = TRUE) : no non-missing arguments to min; returning Inf