How can I remove NA values from a vector?
I have a huge vector which has a couple of
?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.