Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to omit NAs when using the dplyr package Lag function in R?

Tags:

r

dplyr

lag

I have two columns of data and I want to subtract one from the previous row entry of the other. The lag() function in dplyr is perfect for this, but unfortunately I have NAs in the second row of data. This means that when calculating the new value for rows when the previous row has an NA in the second column is resulting in NA's (see below table):

Value 1 Value 2 New Value
NA 2 NA
13 3 11
6 NA 3
6 4 NA
5 4 1

Is there a way to omit then NAs and have it use the last actual value in the second column instead? Like an na.rm=TRUE that can be added? The result would look like:

Value 1 Value 2 New Value
NA 2 NA
13 3 11
6 NA 3
6 4 3
5 4 1
like image 614
Jennifer Weller Avatar asked Oct 16 '25 03:10

Jennifer Weller


1 Answers

One option would be to mutate Value 2 beforehand and fill the NA with the last non-NA value:

df %>%
  tidyr::fill(`Value 2`, .direction = "down")
like image 127
koolmees Avatar answered Oct 17 '25 15:10

koolmees