# Programming Tutorials

## Missing Values in R

By: Karthik Janar

Missing values play an important role in statistics and data analysis. Often, missing values must not be ignored, but rather they should be carefully studied to see if thereâ€™s an underlying pattern or cause for their missingness.

In R, NA is used to represent any value that is â€˜not availableâ€™ or â€˜missingâ€™ (in the statistical sense). In this tutorial, weâ€™ll explore missing values further.

Any operation involving NA generally yields NA as the result. To illustrate, letâ€™s create a vector c(44, NA, 5, NA) and assign it to a variable x.

``x <- c(44, NA, 5, NA)``

Now, letâ€™s multiply x by 3.

``x * 3``
``##  132  NA  15  NA``

Notice that the elements of the resulting vector that correspond with the NA values in x are also NA.

To make things a little more interesting, lets create a vector containing 1000 draws from a standard normal distribution with y <- rnorm(1000).

``y <- rnorm(1000)``

Next, letâ€™s create a vector containing 1000 NAs with z <- rep(NA, 1000).

``z <- rep(NA, 1000)``

Finally, letâ€™s select 100 elements at random from these 2000 values (combining y and z) such that we donâ€™t know how many NAs weâ€™ll wind up with or what positions theyâ€™ll occupy in our final vector â€“ my_data <- sample(c(y, z), 100).

``my_data <- sample(c(y,z),100)``

Letâ€™s first ask the question of where our NAs are located in our data. The is.na() function tells us whether each element of a vector is NA. Call is.na() on my_data and assign the result to my_na.

``my_na <- is.na(my_data)``

Now, print my_na to see what you came up with.

``my_na``
``````##    FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE
##    TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE
##    TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE
##    TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
##   FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE
##    TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE
##   FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE
##   FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE
##    TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE
##   TRUE``````

Everywhere you see a TRUE, you know the corresponding element of my_data is NA. Likewise, everywhere you see a FALSE, you know the corresponding element of my_data is one of our random draws from the standard normal distribution.

In this logical vector tutorial, we introduced the `==` operator as a method of testing for equality between two objects. So, you might think the expression my_data == NA yields the same results as is.na().

``my_data == NA``
``````##    NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
##   NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
##   NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
##   NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
##   NA NA NA NA NA NA NA NA``````

The reason you got a vector of all NAs is that NA is not really a value, but just a placeholder for a quantity that is not available. Therefore the logical expression is incomplete and R has no choice but to return a vector of the same length as my_data that contains all NAs.

So be cautious when using logical expressions anytime NAs might creep in, since a single NA value can derail the entire thing.

Now that we have a vector, my_na, that has a TRUE for every NA and FALSE for every numeric value, we can compute the total number of NAs in our data.

The trick is to recognize that underneath the surface, R represents TRUE as the number 1 and FALSE as the number 0. Therefore, if we take the sum of a bunch of TRUEs and FALSEs, we get the total number of TRUEs.

We can simply call the sum() function on my_na to count the total number of TRUEs in my_na, and thus the total number of NAs in my_data.

``sum(my_na)``
``##  47``

Finally, letâ€™s take a look at the data to convince ourselves that everything â€˜adds upâ€™. Print my_data to the console.

``my_data``
``````##     0.308640011  0.152658355  0.838526330           NA  1.829938956
##              NA  1.053580667 -2.015911304           NA  1.515250166
##             NA           NA           NA  0.004442903           NA
##             NA  1.328032209           NA           NA -0.566152489
##             NA -1.209065883           NA           NA           NA
##             NA           NA           NA  0.587858796           NA
##    0.578621401  1.000485002 -0.631205098           NA  0.834306319
##             NA           NA  0.260352853 -0.738725548 -0.843865535
##   -1.246845096  0.064383360  0.408013650           NA  0.515279838
##    0.486700980           NA           NA           NA           NA
##    1.076405928           NA           NA -1.595102395  0.513133511
##             NA -1.545437626           NA -0.532494817  0.982962820
##    0.369021274           NA -2.376474638  1.207950474 -1.176368630
##             NA  0.461151764  0.470368402 -0.689016269  0.120188849
##             NA -1.297659193 -0.671169981  0.554428281           NA
##             NA           NA -0.648146122           NA           NA
##    1.015285724  0.594523212           NA -3.535239605 -0.925335815
##             NA  0.795023723           NA           NA  0.380860630
##   -1.438414853           NA           NA  0.191796328           NA
##             NA  0.594333902 -0.737094722  0.263012469           NA``````

Now letâ€™s look at a second type of missing value â€“ NaN, which stands for â€˜not a numberâ€™. To generate NaN, try dividing (using a forward slash) 0 by 0 now.

``0/0``
``##  NaN``

Letâ€™s do one more, just for fun. In R, Inf stands for infinity. What happens if you subtract Inf from Inf?

``Inf - Inf``
``##  NaN``

 Most Viewed Articles (in Data Science ) Latest Articles (in Data Science)

Comment on this tutorial