【问题标题】:R issues with merge/rbind/concatenate two data frames合并/rbind/连接两个数据帧的 R 问题
【发布时间】:2014-10-08 13:55:24
【问题描述】:

我是 R 的初学者,所以如果在其他地方提出问题,我提前道歉。这是我的问题:

我有两个数据框 df1 和 df2,它们的行数和列数不同。这两个框架只有一个共同的变量(列),称为“customer_no”。我希望合并的框架仅根据“customer_no”和 df2 中的行匹配记录。两个 data.frames 对于每个 customer_no 都有多行。

我尝试了以下方法:

merged.df <- (df1, df2, by="customer_no",all.y=TRUE)

问题在于,这会将 df1 的值分配给 df2,而它应该为空。我的问题是:

1) 如何告诉命令将不匹配的列留空? 2)如何从合并文件中看到哪一行来自哪个df?我想如果我解决了上述问题,这应该很容易通过空列看到。

我的命令中遗漏了一些东西,但不知道是什么。如果问题已在其他地方得到解答,您是否仍然愿意在这里为 R 初学者用英语重新表述它?

谢谢!

数据示例:

df1:
customer_no  country  year
  10           UK     2001
  10           UK     2002
  10           UK     2003
  20           US     2007
  30           AU     2006


df2:          
customer_no   income
  10            700
  10            800
  10            900 
  30            1000

合并后的文件应如下所示:

merged.df:
 customer_no   income  country   year
     10                  UK      2001
     10                  UK      2002
     10                  UK      2003
     10         700
     10         800
     10         900
     30                  AU      2006
     30         1000

所以: 它将所有列放在一起,它根据相同的 customer_no 在 df1 的最后一个之后添加 df2 的值,并且仅匹配来自 df2 的 customer_no(merged.df 没有 customer_no 20)。此外,它还会将所有其他单元格留空。

在 STATA 中我使用 append 但在 R 中不确定...也许加入?

谢谢!!

【问题讨论】:

  • 添加数据。希望它足够清楚......感谢您的帮助!
  • 这看起来更像是一个 rbind 而不是一个合并/加入,美国条目退出有什么原因吗?
  • DMT,是的,原因是它不在 df2 中。合并的 df 排除了仅在 df1 中(不在 df2 中)的值。

标签: r merge


【解决方案1】:

试试:

df1$id <- paste(df1$customer_no, 1, sep="_")
df2$id <- paste(df2$customer_no, 2, sep="_")

res <- merge(df1, df2, by=c('id', 'customer_no'),all=TRUE)[,-1]
res1 <- res[res$customer_no %in% df2$customer_no,]
res1
 #  customer_no country year income
 #1          10      UK 2001     NA
 #2          10      UK 2002     NA
 #3          10      UK 2003     NA
 #4          10    <NA>   NA    700
 #5          10    <NA>   NA    800
 #6          10    <NA>   NA    900
 #8          30      AU 2006     NA
 #9          30    <NA>   NA   1000

如果你想把NA改成''

 res1[is.na(res1)] <- '' #But, I would leave it as `NA` as there are `numeric` columns.

或者,使用data.table 中的rbindlist(使用原始数据集)

 library(data.table)
 indx <- df1$customer_no %in% df2$customer_no
 rbindlist(list(df1[indx,], df2),fill=TRUE)[order(customer_no)]

 #    customer_no country year income
 #1:          10      UK 2001     NA
 #2:          10      UK 2002     NA
 #3:          10      UK 2003     NA
 #4:          10      NA   NA    700
 #5:          10      NA   NA    800
 #6:          10      NA   NA    900
 #7:          30      AU 2006     NA
 #8:          30      NA   NA   1000

【讨论】:

  • 太棒了!谢谢!!这真是一场噩梦……如此解脱! :)))
  • @Billaus 没问题。很高兴它有帮助。
【解决方案2】:

您也可以使用gtools 包中的smartbind 函数。

require(gtools)
res <- smartbind(df1[df1$customer_no %in% df2$customer_no, ], df2)
res[order(res$customer_no), ]
#      customer_no country year income
#  1:1          10      UK 2001     NA
#  1:2          10      UK 2002     NA
#  1:3          10      UK 2003     NA
#  2:1          10    <NA>   NA    700
#  2:2          10    <NA>   NA    800
#  2:3          10    <NA>   NA    900
#  1:4          30      AU 2006     NA
#  2:4          30    <NA>   NA   1000

【讨论】:

    【解决方案3】:

    试试:

    df1$income = df2$country = df2$year = NA
    rbind(df1, df2)
      customer_no country year income
    1          10      UK 2001     NA
    2          10      UK 2002     NA
    3          10      UK 2003     NA
    4          20      US 2007     NA
    5          30      AU 2006     NA
    6          10    <NA>   NA    700
    7          10    <NA>   NA    800
    8          10    <NA>   NA    900
    9          30    <NA>   NA   1000
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-14
      • 2016-10-30
      • 2019-11-05
      • 2013-12-01
      • 2020-04-07
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多