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 NA values.
How can I remove the NA values so that I can compute the max?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Trying
?max, you’ll see that it actually has ana.rm =argument, set by default toFALSE. (That’s the common default for many other R functions, includingsum(),mean(), etc.)Setting
na.rm=TRUEdoes just what you’re asking for:If you do want to remove all of the
NAs, use this idiom instead:A final note: Other functions (e.g.
table(),lm(), andsort()) haveNA-related arguments that use different names (and offer different options). So ifNA‘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.