Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fill in NA based on the last non-NA value for each group in R [duplicate]

Tags:

r

na

dplyr

My question is I have a dataframe m as below

y1 =c( rep("A",5),rep("B",5))
y2 = rep(c(1:5),2)
y3 = y2
y3[c(2,7,9)]=NA
m = data.frame(y1,y2,y3)

   y1 y2   y3
1   A  1    1
2   A  2 <NA>
3   A  3    3
4   A  4    4
5   A  5    5
6   B  1    1
7   B  2 <NA>
8   B  3    3
9   B  4 <NA>
10  B  5    5

I want to fill in the NA based on the closest non-NA value "in front of" this NA. My output should look like this:

   y1 y2   y3 y4
1   A  1    1  1
2   A  2 <NA>  1
3   A  3    3  3
4   A  4    4  4
5   A  5    5  5
6   B  1    1  1
7   B  2 <NA>  1
8   B  3    3  3
9   B  4 <NA>  3
10  B  5    5  5

Any idea about how to use dplyr to achieve this goal?

like image 490
MYjx Avatar asked Nov 29 '14 21:11

MYjx


People also ask

How to fill NA values with previous values in an R data frame?

How to fill NA values with previous values in an R data frame column? How to fill NA values with previous values in an R data frame column? To fill NA values with next and previous values, we can use na.locf function of zoo package with fromLast = TRUE.

How to fill na based on last observation in R?

In R this is usually solved using the na.locf (Last Observation Carried Forward) function from the zoo package. See also here: Fill in NA based on the last non-NA value for each group in R. Using dplyr window-functions to make trailing values.

How to replace Na with most recent non-NA values in R?

We can do that based on the zoo add-on package. Let’s install and load the package to RStudio: 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.

How to fill NA values with next and previous values in Python?

To fill NA values with next and previous values, we can use na.locf function of zoo package with fromLast = TRUE. This is the situation of a column as shown below −


1 Answers

This may have been answered before, but I don't know if it's been answered in a dplyr context. zoo::na.locf() is your friend:

m %>% group_by(y1) %>% mutate(y4=zoo::na.locf(y3))
like image 83
Ben Bolker Avatar answered Oct 27 '22 09:10

Ben Bolker