R查找数据帧的行,其中某些列与另一列相匹配 [英] R finding rows of a data frame where certain columns match those of another
问题描述
我有一个R问题,我甚至确定如何在一个句子中单词,并且找不到答案。
I have an R question that I'm even sure how to word in one sentence, and couldn't find an answer for this yet.
我有两个我想要相交的数据帧,并找到列值在两个列中匹配的所有行。我已经尝试连接两个intersect()和which()语句与&&,但没有给我我想要的。
I have two data frames that I would like to 'intersect' and find all rows where column values match in two columns. I've tried connecting two intersect() and which() statements with &&, but neither has given me what I want yet.
这是我的意思。假设我有两个数据框:
Here's what I mean. Let's say I have two data frames:
> testData
Email Manual Campaign Bounced Opened Clicked ClickThru Unsubscribed
1 stack@overflow.com EIFLS0LS 1 0 0 0 0 0
2 stack@exchange.com EIFLS0LS 1 0 0 0 0 0
3 data@frame.com EIFLS0LS 1 0 0 0 0 0
4 block@quote.com EIFLS0LS 1 0 0 0 0 0
5 ht@ml.com EIFLS0LS 1 0 0 0 0 0
6 tele@phone.com EIFLS0LS 1 0 0 0 0 0
> testBounced
Email Campaign
1 stack@overflow.com 1
2 stack@overflow.com 2
3 data@frame.com 2
4 block@quote.com 1
5 ht@ml.com 1
6 lap@top.com 1
您可以看到,电子邮件列中有一些值相交,一些来自相交的广告系列。我想要的所有行从testData在哪个BOTH列匹配。
As you can see, there are some values in the column Email that intersect, and some from the column Campaign that intersect. I want all of the rows from testData in which BOTH columns match.
ie:
Email Manual Campaign Bounced Opened Clicked ClickThru Unsubscribed
1 stack@overflow.com EIFLS0LS 1 0 0 0 0 0
2 block@quote.com EIFLS0LS 1 0 0 0 0 0
3 ht@ml.com EIFLS0LS 1 0 0 0 0 0
编辑:
这些列可以更新原始列中的一行。所以我想要的最终输出是:
My goal in finding these columns is to be able to update a row in the original column. So the final output that I would like is:
> testData
Email Manual Campaign Bounced Opened Clicked ClickThru Unsubscribed
1 stack@overflow.com EIFLS0LS 1 1 0 0 0 0
2 stack@exchange.com EIFLS0LS 1 0 0 0 0 0
3 data@frame.com EIFLS0LS 1 0 0 0 0 0
4 block@quote.com EIFLS0LS 1 1 0 0 0 0
5 ht@ml.com EIFLS0LS 1 1 0 0 0 0
6 tele@phone.com EIFLS0LS 1 0 0 0 0 0
如果这是重复,并提前感谢您的帮助!
My apologies if this is a duplicate, and thanks in advance for your help!
EDIT2 ::
我最终只是使用一个循环,没有什么好,但感觉效率不高。数据集足够小,尽快做到这一点。如果任何人有一个快速的R风格的方法,我很高兴看到它!
I ended up just using a for loop, nothing great, but doesn't feel efficient. The dataset was small enough to do it quickly, though. If anyone has a quick, R-style way to do it, I'd be happy to see it!
推荐答案
如果你使用 data.tables
并按照您要匹配的列键入,然后可以一行完成目标:
If you use data.tables
and key by the columns you want to match, then you can accomplish your goal in one line:
tData[tBounce, Bounced := 1L]
br>
library(data.table)
keys <- c("Email", "Campaign")
tData <- data.table(testData, key=keys)
tBounce <- data.table(testBounce, key=keys)
tData[tBounce, Bounced := 1L]
结果:
Results:
tData
Email Manual Campaign Bounced Opened Clicked ClickThru Unsubscribed
1: block@quote.com EIFLS0LS 1 1 0 0 0 0
2: data@frame.com EIFLS0LS 1 0 0 0 0 0
3: ht@ml.com EIFLS0LS 1 1 0 0 0 0
4: stack@exchange.com EIFLS0LS 1 0 0 0 0 0
5: stack@overflow.com EIFLS0LS 1 1 0 0 0 0
6: tele@phone.com EIFLS0LS 1 0 0 0 0 0
>
这篇关于R查找数据帧的行,其中某些列与另一列相匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!