How to Deal with Missing Values in R

It can happen that your dataset is not complete; when a piece of information is not available, we call it a missing value. In R, missing values are coded with the symbol NA. To identify missing values in your dataset, the function to use is is.na().

First, let’s create a small dataset:

Name <- c("John", "Tim", NA)
Sex <- c("men", "men", "women")
Age <- c(45, 53, NA)
dt <- data.frame(Name, Sex, Age)

Here is our dataset, called dt:

dt 
Name   Sex Age
1 John   men  45
2  Tim   men  53
3  <NA> women  NA

Now we will check for missing values in the dataset. The function returns TRUE for every cell that is missing:

is.na(dt)
Name    Sex   Age
FALSE FALSE FALSE
FALSE FALSE FALSE
TRUE  FALSE  TRUE

You can also find the sum and the percentage of missing values in your dataset with the code below:

sum(is.na(dt))
mean(is.na(dt))
2
0.2222222

So our dataset has 2 missing values, which is about 22% of all the cells.

When you import a dataset from another statistical application, the missing values might be coded with a number, for example 99. In order to let R know that it is a missing value, you need to recode it:

dt$Age[dt$Age == 99] <- NA

Another useful function in R for dealing with missing values is na.omit(), which deletes incomplete observations (rows containing at least one NA).

Let’s look at another example, first creating another small dataset:

Name <- c("John", "Tim", NA)
Sex <- c("men", NA, "women")
Age <- c(45, 53, NA)
dt <- data.frame(Name, Sex, Age)

Here is the dataset, again called dt:

dt
Name Sex Age
John men  45
Tim  <NA>  53
<NA> women NA

Now we will use the function to remove the incomplete observations:

na.omit(dt)
Name Sex Age
John men  45

Only John’s row remains, because it is the only observation with no missing values.

This was an introduction to dealing with missing values. To learn how to impute missing data, please read this post.

1 Comment

  1. H
    HistorySquared December 12, 2015

    This would be better with a more advanced discussion, such as discussing imputing missing values for time series with multiple imputation or splines. It’s a good introduction though.

    Reply

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.