Posted By: Anonymous
I have a huge vector which has a couple of
NA values, and I’m trying to find the max value in that vector (the vector is all numbers), but I can’t do this because of the
How can I remove the
NA values so that I can compute the max?
?max, you’ll see that it actually has a
na.rm = argument, set by default to
FALSE. (That’s the common default for many other R functions, including
na.rm=TRUE does just what you’re asking for:
d <- c(1, 100, NA, 10) max(d, na.rm=TRUE)
If you do want to remove all of the
NAs, use this idiom instead:
d <- d[!is.na(d)]
A final note: Other functions (e.g.
NA-related arguments that use different names (and offer different options). So if
NA‘s cause you problems in a function call, it’s worth checking for a built-in solution among the function’s arguments. I’ve found there’s usually one already there.