Missing Values

Missing values are denoted by NA or NaN for q undefined mathematical operations.

  1. is.na() is used to test objects if they are NA
  2. is.nan() is used to test for NaN
  3. NA values have a class also, so there are integer NA, character NA, etc.
  4. A NaN value is also NA but the converse is not true

You can create a vector with NAs in it.

 

x <- c(1, 2, NA, 10, NA)
x

Output:

[1]  1  2 NA 10 NA

is.na() is a function that return a logical vector indicating which elements are NA. It returns TRUE or FALSE.

is.na(x)

Output:

[1] FALSE FALSE  TRUE FALSE  TRUE

x <- c(1, 2, NA, 10, NA, 0/0)

Output:

[1]   1   2  NA  10  NA NaN

Return a logical vector indicating which elements are NaN

is.nan(x)

Output:

[1] FALSE FALSE FALSE FALSE FALSE  TRUE

Removing NA Values:

Extract elements excepts NA values.

 

x <- c(100, 200, NA, 300,NA, 400)
b <- is.na(x)
x[!b]

Extract elements excepts NAN values.

 

y <- c(100, 200, NA, 300,NA, 400, 0/0)
y
y[!is.na(y)]

Output:

[1] 100 200 300 400

There is another useful function called complete.cases(). You can use them like below.

 

x <- c(100, 200, NA, 400, NA, 500)
y <- c("x", "y", NA, "z", NA, "w")
c<-complete.cases(x, y)

Output:

> x
[1] 100 200 400 500
> y
[1] "x" "y" "z" "w"

You can use complete.cases on data frames too.

 

x <- c(100, 200, NA, 400, NA, 500)
y <- c("x", "y", NA, "z", NA, "w")
z<- c("a","b","c","d","e","f")
d<-data.frame(x,y,z)
d[complete.cases(d),]

Output:

   x y z
1 100 x a
2 200 y b
4 400 z d
6 500 w f

Data Frames

R – Operators