Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to detect if vector has at least 1 NA?

Tags:

r

na

What is the fastest way to detect if a vector has at least 1 NA in R? I've been using:

sum( is.na( data ) ) > 0 

But that requires examining each element, coercion, and the sum function.

like image 567
SFun28 Avatar asked Jul 01 '11 18:07

SFun28


People also ask

How do I check if a vector has Na in R?

To test if a value is NA, use is.na(). The function is.na(x) returns a logical vector of the same size as x with value TRUE if and only if the corresponding element in x is NA.

How would you check whether a vector contains missing values?

To identify missing values use is.na() which returns a logical vector with TRUE in the element locations that contain missing values represented by NA .

How do you get rid of Na in a vector?

We can remove those NA values from the vector by using is.na(). is.na() is used to get the na values based on the vector index. ! is.na() will get the values except na.

How do you check if a certain value is in a vector in R?

%in% operator can be used in R Programming Language, to check for the presence of an element inside a vector. It returns a boolean output, evaluating to TRUE if the element is present, else returns false.


1 Answers

I'm thinking:

any(is.na(data)) 

should be slightly faster.

like image 191
Sacha Epskamp Avatar answered Sep 30 '22 01:09

Sacha Epskamp