Introduction to R and RStudio

Chapter 7 Missing Values

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 lesson, 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
## [1] 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
##   [1] FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE  TRUE
##  [18] FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
##  [35]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
##  [52] FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
##  [69]  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE
##  [86] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  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 our previous discussion of logical operators, 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(). Give it a try.

my_data == NA
##   [1] 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
##  [36] 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
##  [71] 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.

Don’t worry if that’s a little confusing. The key takeaway is to be cautious when using logical expressions anytime NAs might creep in, since a single NA value can derail the entire thing.

So, back to the task at hand. 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.

Let’s give that a try here. 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. Don’t assign the result to a new variable.

sum(my_na)
## [1] 53

Pretty cool, huh? Finally, let’s take a look at the data to convince ourselves that everything ‘adds up’. Print my_data to the console.

my_data
##   [1] -1.41093582 -0.87448270 -1.70441919          NA          NA -0.12317984  0.63936005          NA
##   [9]          NA -0.50793003          NA  0.24945118          NA  0.06624340  1.71488786          NA
##  [17]          NA -0.04238945 -0.82203675          NA -0.15186434          NA          NA  1.02963665
##  [25]  0.16163570  0.44463407  0.06242177  1.69290984          NA          NA          NA          NA
##  [33] -0.73356664          NA          NA          NA          NA          NA          NA          NA
##  [41]  0.32638674  0.45153726  1.79007796 -2.54271501 -0.69892452          NA          NA          NA
##  [49]          NA -0.84943464          NA -0.62954961 -1.52604085          NA          NA -0.79226233
##  [57]  0.43344734  1.42163725          NA  1.31800048  0.34423659  2.44223168          NA          NA
##  [65]          NA          NA -1.54730103          NA          NA          NA  0.74644038 -0.64221405
##  [73]          NA          NA -1.18841083          NA  1.05196531          NA          NA          NA
##  [81]          NA          NA -1.08898598          NA  0.33396396 -0.76877538          NA -0.56644783
##  [89] -0.58328668 -0.06239196  0.93477354 -0.00541329          NA          NA          NA          NA
##  [97]  1.03854389  1.12720535          NA          NA

Now that we’ve got NAs down pat, 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
## [1] 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
## [1] NaN

You’ve successfully completed this lesson!