与data.table合并时防止重复的列 [英] Preventing duplicate columns when merging with data.table

查看:86
本文介绍了与data.table合并时防止重复的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据表,它们的列名部分相似:

I have two data tables which have partly similar column names:

   dfA <- read.table(
  text = "A   B   C   D   E   F   G   iso   year   matchcode
  1   0   1   1   1   0   1   0   NLD   2010   NLD2010
  2   1   0   0   0   1   0   1   NLD   2014   NLD2014
  3   0   0   0   1   1   0   0   AUS   2010   AUS2010
  4   1   0   1   0   0   1   0   AUS   2006   AUS2006
  5   0   1   0   1   0   1   1   USA   2008   USA2008
  6   0   0   1   0   0   0   1   USA   2010   USA2010
  7   0   1   0   1   0   0   0   USA   2012   USA2012
  8   1   0   1   0   0   1   0   BLG   2008   BLG2008
  9   0   1   0   1   1   0   1   BEL   2008   BEL2008
  10   1   0   1   0   0   1   0  BEL   2010   BEL2010",
  header = TRUE
)

   dfB <- read.table(
  text = "A   B   C   D   H   I   J   iso   year   matchcode
  1   0   1   1   1   0   1   0   NLD   2009   NLD2009
  2   1   0   0   0   1   0   1   NLD   2014   NLD2014
  3   0   0   0   1   1   0   0   AUS   2011   AUS2011
  4   1   0   1   0   0   1   0   AUS   2007   AUS2007
  5   0   1   0   1   0   1   1   USA   2007   USA2007
  6   0   0   1   0   0   0   1   USA   2011   USA2010
  7   0   1   0   1   0   0   0   USA   2013   USA2013
  8   1   0   1   0   0   1   0   BLG   2007   BLG2007
  9   0   1   0   1   1   0   1   BEL   2009   BEL2009
  10   1   0   1   0   0   1   0  BEL   2012   BEL2012",
  header = TRUE
)
library(data.table)
setDT(dfA)
setDT(dfB)

要合并data.tables,我将执行以下操作:

To merge the data.tables I will do the following:

dfA <- dfA[dfB, on = .(iso, year), roll = "nearest", nomatch = 0]

但是,除了所需的重复列matchcode之外,这还将创建不需要的重复列A, B, C, D.由于我需要进行的合并数量众多,因此会变得太杂乱.

This will however, apart from the desired duplicate column matchcode also create the undesired duplicate columns A, B, C, D. Because of the number of merges I need to do, that would get too messy.

是否有一种方法可以在不显式引用的情况下从合并过程中排除重复的列?如果没有,我该如何通过显式引用它们来做到这一点.如果不是,是否可以在不明确引用重复项的情况下将其删除?例如,删除所有看起来像"i.columnname"的列?

Is there a way to exclude duplicate columns from the merging process without explicitly referring to them? If not, how can I do so by explicitly referring to them. If not, can I remove them afterwards without explicitly referring to the duplicates? For example by removing all columns which look like `i.columnname' ?

首选输出如下:

#    A B C D E F G iso year matchcodeA H I J matchcodeB
# 1: 1 0 0 0 1 0 1 NLD  2014  NLD2014  1 0 1    NLD2014
# 2: 0 0 0 1 1 0 0 AUS  2011  AUS2010  1 0 0    AUS2011
# 3: 1 0 1 0 0 1 0 AUS  2007  AUS2006  0 1 0    AUS2007
# 4: 0 0 1 0 0 0 1 USA  2011  USA2010  0 0 1    USA2010
# 5: 0 1 0 1 0 0 0 USA  2013  USA2012  0 0 0    USA2013
# 6: 0 1 0 1 1 0 1 BEL  2009  BEL2008  1 0 1    BEL2009
# 7: 0 1 1 1 0 1 0 NLD  2009  NLD2010  0 1 0    NLD2009
# 8: 0 1 0 1 0 1 1 USA  2007  USA2008  0 1 1    USA2007
# 9: 0 1 0 1 0 0 0 USA  2011  USA2012  0 0 1    USA2010
#10: 1 0 1 0 0 1 0 BEL  2009  BEL2010  1 0 1    BEL2009

推荐答案

我们可以创建与intersecgt

nm1 <- intersect(names(dfA), names(dfB))

然后,使用setdiff查找在'dfB'中而不在'nm1'中找到的列名称,同时包括连接列'iso''year'和'matchcode'

then, use setdiff to find the column names that are found in 'dfB' and not in the 'nm1' while including the joining columns 'iso' 'year' as well as the 'matchcode'

nm2 <- c(setdiff(names(dfB), nm1), "iso", "year", "matchcode")

现在,我们进行加入

out <- dfA[dfB[, ..nm2], on = .(iso, year), roll = "nearest", nomatch = 0]
setnames(out, c('matchcode', 'i.matchcode'), c('matchcodeA', 'matchcodeB'))

这篇关于与data.table合并时防止重复的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆