R: fill missing value with prior values [duplicate]

吃可爱长大的小学妹 提交于 2019-12-12 02:08:30

问题


I have a dataframe that looks like this:

d <- data.frame(county = c("Abilene", rep(NA, 5), "Cook", rep(NA, 4), "Blah", NA, "Allegheny", rep(NA, 3)))

      county
1    Abilene
2       <NA>
3       <NA>
4       <NA>
5       <NA>
6       <NA>
7       Cook
8       <NA>
9       <NA>
10      <NA>
11      <NA>
12      Blah
13      <NA>
14 Allegheny
15      <NA>
16      <NA>
17      <NA>

I want to fill in the <NA> with the value of the previous non-missing county name. In other words, I want to end up with this:

  county
1       Abilene
2       Abilene
3       Abilene
4       Abilene
5       Abilene
6       Abilene
7       Cook
8       Cook
9       Cook
10      Cook
11      Cook
12      Blah
13      Blah
14      Allegheny
15      Allegheny
16      Allegheny
17      Allegheny

So far, I have been looping over every value in d$county, updating a temporary variable with the name of every non-empty county value, and refilling each cell. This is very slow with a large dataframe. I would prefer to do this in dplyr, though am open to any other solution as well.


回答1:


Using tidyr we can use fill(data, vars):

library(tidyr)
fill(d, county)



回答2:


We can use na.locf

library(zoo)
na.locf(d)


来源:https://stackoverflow.com/questions/42570024/r-fill-missing-value-with-prior-values

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