R:用先前的值填充缺失的值 [英] R: fill missing value with prior values
本文介绍了R:用先前的值填充缺失的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的数据框:
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>
我想用先前的非缺失县名的值填写<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
到目前为止,我一直在遍历d$county
中的每个值,使用每个非空县值的名称更新一个临时变量,并重新填充每个单元格.对于大数据帧,这非常慢.我更愿意在dplyr
中执行此操作,尽管也可以接受其他任何解决方案.
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.
推荐答案
使用tidyr
,我们可以使用fill(data, vars)
:
library(tidyr)
fill(d, county)
这篇关于R:用先前的值填充缺失的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文