通过匹配另一个data.table的列来对数据表进行子集 [英] Subset a data.table by matching columns of another data.table

查看:126
本文介绍了通过匹配另一个data.table的列来对数据表进行子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个解决方案,使用另一个数据表中某些列的匹配值对数据表进行子集化。

I have been searching for a solution for subsetting a data table using matching values for certain columns in another data table.

以下是示例:

set.seed(2)

dt <- 
        data.table(a = 1:10, 
                   b = rnorm(10), 
                   c = runif(10), 
                   d = letters[1:10])

dt2 <- 
        data.table(a = 5:20, 
                   b = rnorm(16), 
                   c = runif(16), 
                   d = letters[5:20])

需要:

> dt2

 1:  5 -2.311069085 0.62512173 e
 2:  6  0.878604581 0.26030004 f
 3:  7  0.035806718 0.85907312 g
 4:  8  1.012828692 0.43748800 h
 5:  9  0.432265155 0.38814476 i
 6: 10  2.090819205 0.46150111 j

从第二数据表返回,其中a和d匹配,即使b和c不匹配。

where I have the rows returned from the second data table where a and d match even though b and c may not. The real data are mutually exclusive, and I need to match on three columns.

推荐答案

我们可以使用 %in%以匹配列和子集。

We can use %in% to match the columns and subset accordingly.

dt2[a %in% dt$a & d %in% dt$d]
#    a           b         c d
#1:  5 -2.31106908 0.6251217 e
#2:  6  0.87860458 0.2603000 f
#3:  7  0.03580672 0.8590731 g
#4:  8  1.01282869 0.4374880 h
#5:  9  0.43226515 0.3881448 i
#6: 10  2.09081921 0.4615011 j

这篇关于通过匹配另一个data.table的列来对数据表进行子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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