如何用其他两个数据框替换一个数据框的NA元素

时间:2019-07-01 20:11:51

标签: r dataframe

我有一个数据框,称为to_add:

 1999 2000 2001 2002
3   NA   NA   NA   NA
6   NA   NA   NA   NA

和另外2个数据帧,series_J6和series_V2

 1999 2000 2001 2002
2   NA   NA   23   NA
5   NA   NA   NA   NA
 1999 2000 2001 2002
3   NA   NA   25   12
6   NA   NA   25   NA

我希望先用series_J6中的对应元素替换to_add中的Na值,然后再用series_V2中的Na值替换(以便to_add [1,3]为23而不是25)。这是我的代码:

  to_add[1,] = ifelse(is.na(to_add[1,]), series_J6[1, ], to_add[1,])
  to_add[2,] = ifelse(is.na(to_add[2,]), series_J6[2, ], to_add[2,])
  to_add[1,] = ifelse(is.na(to_add[1,]), series_V2[1, ], to_add[1,])
  to_add[2,] = ifelse(is.na(to_add[2,]), series_V2[2, ], to_add[2,])

但是很笨重。有更好的方法吗?

2 个答案:

答案 0 :(得分:0)

我们将数据集放在listcoalesce

library(tidyverse)
list(df1, df2, df3) %>%
     reduce(coalesce)

数据

df1 <- structure(list(`1999` = c(NA, NA), `2000` = c(NA, NA), `2001` = c(NA, 
NA), `2002` = c(NA, NA)), class = "data.frame", row.names = c("3", 
"6"))

df2 <- structure(list(`1999` = c(NA, NA), `2000` = c(NA, NA), `2001` = c(23L, 
NA), `2002` = c(NA, NA)), class = "data.frame", row.names = c("2", 
"5"))

df3 <- structure(list(`1999` = c(NA, NA), `2000` = c(NA, NA), `2001` = c(25L, 
25L), `2002` = c(12L, NA)), class = "data.frame", row.names = c("3", 
"6"))

答案 1 :(得分:0)

这是基本的R方式。我们可以编写一个函数来检查要替换的数据框中的NA索引,然后用替换数据框中的相应索引替换这些索引。这利用data.frame的出色功能将矩阵用作索引器。

df1 <- structure(list(`1999` = c(NA, NA), `2000` = c(NA, NA), `2001` = c(NA, NA), `2002` = c(NA, NA)), class = "data.frame", row.names = c("3", "6"))
df2 <- structure(list(`1999` = c(NA, NA), `2000` = c(NA, NA), `2001` = c(23L, NA), `2002` = c(NA, NA)), class = "data.frame", row.names = c("2", "5"))
df3 <- structure(list(`1999` = c(NA, NA), `2000` = c(NA, NA), `2001` = c(25L, 25L), `2002` = c(12L, NA)), class = "data.frame", row.names = c("3", "6"))

replace_na_cells <- function(df, replace) {
  replace_idxs <- which(is.na(as.matrix(df)), arr.ind = TRUE)
  df[replace_idxs] <- replace[replace_idxs]
  df
}

Reduce(replace_na_cells, list(df1, df2, df3))
#>   1999 2000 2001 2002
#> 3   NA   NA   23   12
#> 6   NA   NA   25   NA

reprex package(v0.3.0)于2019-07-01创建