How to fill down values in R data frame

There are at least two ways how to fill down values in R data frame columns. The fastest way is to use zoo package function na.locf that is combined with dplyr function mutate. Besides, sometimes in R, it is necessary to replace NA with 0. All of those things in this post.

Here are my data frame and code for that.

As a result, columns should be filled like this.

Fill data frame values with fill function from the tidyr package

Tidyr has a handy function fill. This approach is easy to understand but pretty slow in large data frames with more than 100 000 rows.
In this case, I’m interested to fill values in all columns. To save some time, you can call all data frame columns with function names or colnames.

Another option is to select all columns with dplyr function everything.

Fill R data frame NA values with 0

In some cases, there is necessary to replace NA with 0. As you can see in the previous figure, some of the columns start with NA, and that might be logical. In R, you can do it by using square brackets.

Fill R data frame values with na.locf function from zoo package

This approach is the fastest. There is a handy zoo package function na.locf that replaces NA value with the most recent non-NA value. In combination with mutate it can replace existing columns. It is not so flexible as tidyr fill where you can change filling options but on large data frames at least a few times faster.

If you are new to R, check out this other post on how to switch from Excel to R.

0 comments on “How to fill down values in R data frame

Leave a Reply

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