根据另一张表中的两列选择R表中的行。

huangapple go评论110阅读模式
英文:

Select rows from R table based on two columns in another table

问题

我有两个表格:table1和table2,其中table1比table2大得多,但table2不完全包含在table1中。每个表格中还有两个ID列 - ID1和ID2。我想获取table1和table2中两个ID列匹配的行。如果一个ID的配对在一个表格中而不在另一个表格中,那么这一行就不应返回。

我尝试了以下代码:t1[which(t1$ID1 == t2$ID1 & t1$ID2 == t2$ID2,但它显示“长对象长度不是短对象长度的倍数”。有什么想法吗?

英文:

I have two tables; table1 and table2, where table1 is much bigger than table2, but table2 is not fully contained in table1. I also have two ID columns - ID1 and ID2 in each table. I want to obtain the rows in table1 and table 2 in which the two ID columns coincide. If a pairing of ID's is in one table and not the other then the row should not be returned.

I tried t1[which(t1$ID1 == t2$ID1 & t1$ID2 == t2$ID2

It said that the longer object length is not a multiple of shorter object length. Any ideas?

答案1

得分: 3

使用 dplyr::semi_join()(并借用 @thesixmax 的示例数据):

  1. library(dplyr)
  2. table1 %>%
  3. semi_join(table2, by = c("ID_1", "ID_2"))
  4. # ID_1 ID_2 val
  5. # 1 0_2 1_2 2
  6. # 2 0_4 1_4 4
  7. table2 %>%
  8. semi_join(table1, by = c("ID_1", "ID_2"))
  9. # ID_1 ID_2 val
  10. # 1 0_2 1_2 1
  11. # 2 0_4 1_4 2
英文:

With dplyr::semi_join() (and borrowing @thesixmax’s example data):

  1. library(dplyr)
  2. table1 %>%
  3. semi_join(table2, by = c("ID_1", "ID_2"))
  4. # ID_1 ID_2 val
  5. # 1 0_2 1_2 2
  6. # 2 0_4 1_4 4
  7. table2 %>%
  8. semi_join(table1, by = c("ID_1", "ID_2"))
  9. # ID_1 ID_2 val
  10. # 1 0_2 1_2 1
  11. # 2 0_4 1_4 2

答案2

得分: 2

简单的示例:

  1. table1 <- data.frame(
  2. "ID_1" = c("0_1", "0_2", "0_3", "0_4", "0_5"),
  3. "ID_2" = c("1_1", "1_2", "1_3", "1_4", "1_5"),
  4. val = c(1, 2, 3, 4, 5)
  5. )
  6. table2 <- data.frame(
  7. "ID_1" = c("0_2", "0_4", "0_6", "0_7", "0_8", "0_9", "0_10"),
  8. "ID_2" = c("1_2", "1_4", "1_6", "1_7", "1_8", "1_9", "1_10"),
  9. val = c(1, 2, 3, 4, 5, 6, 7)
  10. )
  11. 使用基本的R解决方案:
  12. ids1 <- which(interaction(table1[,c("ID_1", "ID_2")]) %in%
  13. interaction(table2[,c("ID_1", "ID_2")]))
  14. ids2 <- which(interaction(table2[,c("ID_1", "ID_2")]) %in%
  15. interaction(table1[,c("ID_1", "ID_2")]))
  16. overlap1 <- table1[ids1,]
  17. overlap2 <- table2[ids2,]
英文:

Simple repex:

  1. table1 &lt;- data.frame(
  2. &quot;ID_1&quot; = c(&quot;0_1&quot;, &quot;0_2&quot;, &quot;0_3&quot;, &quot;0_4&quot;, &quot;0_5&quot;),
  3. &quot;ID_2&quot; = c(&quot;1_1&quot;, &quot;1_2&quot;, &quot;1_3&quot;, &quot;1_4&quot;, &quot;1_5&quot;),
  4. val = c(1, 2, 3, 4, 5)
  5. )
  6. table2 &lt;- data.frame(
  7. &quot;ID_1&quot; = c(&quot;0_2&quot;, &quot;0_4&quot;, &quot;0_6&quot;, &quot;0_7&quot;, &quot;0_8&quot;, &quot;0_9&quot;, &quot;0_10&quot;),
  8. &quot;ID_2&quot; = c(&quot;1_2&quot;, &quot;1_4&quot;, &quot;1_6&quot;, &quot;1_7&quot;, &quot;1_8&quot;, &quot;1_9&quot;, &quot;1_10&quot;),
  9. val = c(1, 2, 3, 4, 5, 6, 7)
  10. )

A solution using base R:

  1. ids1 &lt;- which(interaction(table1[,c(&quot;ID_1&quot;, &quot;ID_2&quot;)]) %in%
  2. interaction(table2[,c(&quot;ID_1&quot;, &quot;ID_2&quot;)]))
  3. ids2 &lt;- which(interaction(table2[,c(&quot;ID_1&quot;, &quot;ID_2&quot;)]) %in%
  4. interaction(table1[,c(&quot;ID_1&quot;, &quot;ID_2&quot;)]))
  5. overlap1 &lt;- table1[ids1,]
  6. overlap2 &lt;- table2[ids2,]

huangapple
  • 本文由 发表于 2023年2月24日 08:59:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定