问题
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