Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if entire vector has no values other than NA (or NAN) in R?

Tags:

r

How to check if entire vector has no values other than NA (or NAN) in R ?

If I use is.na it returns a vector of TRUE / FALSE.

I need to check if there is single not NA element or not.

like image 531
Dimitar Slavchev Avatar asked Feb 23 '12 16:02

Dimitar Slavchev


People also ask

How to identify the NaN values in R data?

However, if we try to run an invalid computation (e.g. 0 / 0), R returns NaN: If we have a complex vector, data frame or matrix, it might be complicated to identify the NaN values in our data. In such a case, we can apply the is.nan function. The is.nan function returns a logical vector or matrix, which indicates the NaN positions in our data.

How do you find the NaN value of a complex vector?

If we have a complex vector, data frame or matrix, it might be complicated to identify the NaN values in our data. In such a case, we can apply the is.nan function. The is.nan function returns a logical vector or matrix, which indicates the NaN positions in our data.

How to check whether a vector contains an NA value or not?

How to check whether a vector contains an NA value or not in R? An NA value in R represents “Not Available” that means missing value. If a vector has even one NA value then the calculations for that vector becomes a little difficult because we will either have to remove that NA, replace it or neglect it during the calculations.

How to return values in R that are not NA values?

You can use the following syntax to return values in R that are not NA values: The following examples show how to use this syntax with both vectors and data frames in R. #create vector x <- c (1, 24, NA, 6, NA, 9) #return only values that are not NA x <- x [!is.na(x)] [1] 1 24 6 9


1 Answers

The function all(), when passed a Boolean vector, will tell you whether all of the values in it are TRUE:

> all(is.na(c(NA, NaN))) [1] TRUE > all(is.na(c(NA, NaN, 1))) [1] FALSE 
like image 83
Josh O'Brien Avatar answered Sep 26 '22 00:09

Josh O'Brien