考虑以下data.frame:
df1 <- data.frame(index = c(1,1,1,2,2,2),
country = c("AR","AR", "DE", "CH", "AR", "DE"),
type = c("CoL", "CPI", "GDP", "CPI", "CPI", "GDP"),
value = c(11.12, 53.32, 10.42, 120.23, NA, NA))
> df1
index country type value
1 1 AR CoL 11.12
2 1 AR CPI 53.32
3 1 DE GDP 10.42
4 2 CH CPI 120.23
5 2 AR CPI NA
6 2 DE GDP NA
如何用先前索引的相应值替换NA,但仅当除索引之外的所有其他属性相互匹配时,结果将如下所示:
> df2
index country type value
1 1 AR CoL 11.12
2 1 AR CPI 53.32
3 1 DE GDP 10.42
4 2 CH CPI 120.23
5 2 AR CPI 53.32
6 2 DE GDP 10.42
由于实际的data.frames非常大,我打算避免使用for循环多次筛选数据 谢谢大家!