Replace NA with Last Observed Value in R (Example)

 

In this R tutorial you’ll learn how to replace NAs with the previous non-NA value.

Table of contents:

Let’s dive into it.

 

Creation of Example Data

For the example of this tutorial, we will use a very simple numeric vector:

x <- c(1, 2, NA, NA, NA, 3, NA, 4)    # Create example data
x                                     # Print example data
# 1  2 NA NA NA  3 NA  4

As you can see based on the previous R code, our example vector contains values ranging from 1 to 4 with NA values in between.

 

Example: Replace NA with latest non-NA Value in R

Let’s assume that we want to replace these NA values with the latest observed value in our data vector. We can do that based on the zoo add-on package. Let’s install and load the package to RStudio:

install.packages("zoo")               # Install zoo package
library("zoo")                        # Load zoo package

The zoo R package contains the na.locf function, which is a generic function for replacing each NA with the most recent non-NA value prior to it. Let’s do this in practice:

na.locf(x)                            # Apply na.locf function
# 1 2 2 2 2 3 3 4

Based on the previous output of the RStudio console you can see that all NA values were replaced with the previously observed number.

 

Video, Further Resources & Summary

I have recently published a video on my YouTube channel, which explains the R programming codes of this article. You can find the video below:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Besides that, you might want to have a look at the related articles on my website. Please find a selection of interesting articles here:

 

In summary: In this R tutorial you learned how to fill missing values using the previous observation. In case you have further questions, let me know in the comments section below.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


2 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top