首页 文章

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

提问于
浏览
3

我将根据样本数据向您解释我的问题 . 这是第一个表 (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

这是一个 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")

和第二个表 (df2)

x x1
1 2 10
2 3 60

dput()

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

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

首先我使用 gtools 库中的 smartbind() 函数:

library(gtools)
data <- smartbind(df1, df2)

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

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

所以我想填写df2行中出现的所有NA值,如果 x is the same ,则填充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

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

Is there any more ELEGANT way to do it rather then joining two data frames and then using rbind()

2 回答

  • 1

    或使用 tidyverse

    library(tidyverse)
    df1 %>% 
       select(-x1) %>% 
       right_join(df2) %>%
       bind_rows(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
    #6 2 10 <NA> NA
    #7 3 60    b 13
    

    或者 data.table

    nm1 <- setdiff(names(df1), c('x', 'x1'))
    setDT(df2)[df1, (nm1) := mget(nm1), on = .(x)]
    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
    
  • 5

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

    df2 = merge(df2,df1[,c("x","y","z")],by="x",all.x=T)
    

    然后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
    

相关问题