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

情到浓时终转凉″ 提交于 2019-11-27 03:26:02

问题


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?


回答1:


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))


来源:https://stackoverflow.com/questions/27207162/fill-in-na-based-on-the-last-non-na-value-for-each-group-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!