R合并/rbind/连接两个数据帧的问题

R合并/rbind/连接两个数据帧的问题,r,merge,R,Merge,我是R的初学者,所以如果有人问我这个问题,我会提前道歉。这是我的问题: 我有两个数据帧,df1和df2,行数和列数不同。这两个框架只有一个共同的变量(列),称为“customer_no”。我希望合并的框架仅根据“customer_no”和df2中的行匹配记录。两个data.frame都有多行用于每个customer_no 我尝试了以下方法: merged.df <- (df1, df2, by="customer_no",all.y=TRUE) 合并文件应如下所示: merged.df:

我是R的初学者,所以如果有人问我这个问题,我会提前道歉。这是我的问题:

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

我尝试了以下方法:

merged.df <- (df1, df2, by="customer_no",all.y=TRUE)
合并文件应如下所示:

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中不确定…也许是join

谢谢

试试看:

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
或者,使用
数据表中的
rbindlist
(使用原始数据集)

库(data.table)

indx您还可以使用
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
require(gtools)
res试试:

df1$income=df2$country=df2$year=NA
rbind(df1,df2)
客户无国家/地区年度收入
1 10英国2001年NA
2 10英国2002年北美
3 10英国2003北美
4 20美国2007不适用
5 30 AU 2006 NA
610NA 700
7 10 NA 800
8 10 NA 900
9 30 NA 1000

添加数据。希望足够清楚…谢谢你的帮助!!这看起来更像是一个rbind,而不是一个merge/join,是否有美国条目退出的原因?DMT,是的,原因是它不在df2中。合并的df排除了仅在df1中的值(不在df2中)。太棒了!谢谢这真是一场噩梦……真是一种解脱!:)@比劳斯没问题。很高兴这有帮助。
 res1[is.na(res1)] <- '' #But, I would leave it as `NA` as there are `numeric` columns.
 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
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
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