rbind两个表并用具有相同变量的值填充NA的行

Mal*_*l_a 3 r

我将根据样本数据向您解释我的问题.这是第一个表(df1):

  x x1 y  z
1 1 10 a 11
2 3 11 b 13
3 5 10 c 15
4 7 11 d 17
5 9 10 e 19
Run Code Online (Sandbox Code Playgroud)

这是一个dput()版本:

structure(list(x = c(1, 3, 5, 7, 9), x1 = c(10, 11, 10, 11, 10
), y = structure(1:5, .Label = c("a", "b", "c", "d", "e"), class = "factor"), 
    z = c(11, 13, 15, 17, 19)), .Names = c("x", "x1", "y", "z"
), row.names = c(NA, -5L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

和第二个表(df2):

  x x1
1 2 10
2 3 60
Run Code Online (Sandbox Code Playgroud)

dput():

structure(list(x = c(2, 3), x1 = c(10, 60)), .Names = c("x", 
"x1"), row.names = c(NA, -2L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

我现在需要绑定这两个表的行,并使用df1中的值填充缺少的列值.让我在这两个表的基础上解释你.

起初,我使用smartbind()功能从gtools库:

library(gtools)
data <- smartbind(df1, df2)
Run Code Online (Sandbox Code Playgroud)

我得到的结果看起来像那样:

 x x1    y  z
 1 10    a 11
 3 11    b 13
 5 10    c 15
 7 11    d 17
 9 10    e 19
 2 10 <NA> NA
 3 60 <NA> NA
Run Code Online (Sandbox Code Playgroud)

所以我想填写df2行中出现的所有NA值,如果x相同则用df1值.在这种情况下,它看起来像这样:

 x x1    y  z
 1 10    a 11
 3 11    b 13
 5 10    c 15
 7 11    d 17
 9 10    e 19
 2 10 <NA> NA
 3 60    b 13
Run Code Online (Sandbox Code Playgroud)

在我的原始数据集中,我确实有大约280列!感谢帮助

有没有更多的ELEGANT方法来做它而不是连接两个数据帧然后使用rbind()

小智 5

首先,您可以从df1合并缺少的df2列,只保留额外的列(y以及df1中z的键列x):

df2 = merge(df2,df1[,c("x","y","z")],by="x",all.x=T)
Run Code Online (Sandbox Code Playgroud)

然后rbind df1和df2:

> rbind(df1,df2)
  x x1    y  z
1 1 10    a 11
2 3 11    b 13
3 5 10    c 15
4 7 11    d 17
5 9 10    e 19
6 2 10 <NA> NA
7 3 60    b 13
Run Code Online (Sandbox Code Playgroud)