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

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

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 的示例数据):

library(dplyr)

table1 %>%
  semi_join(table2, by = c("ID_1", "ID_2"))
#   ID_1 ID_2 val
# 1  0_2  1_2   2
# 2  0_4  1_4   4

table2 %>%
  semi_join(table1, by = c("ID_1", "ID_2"))
#   ID_1 ID_2 val
# 1  0_2  1_2   1
# 2  0_4  1_4   2
英文:

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

library(dplyr)

table1 %>%
  semi_join(table2, by = c("ID_1", "ID_2"))
#   ID_1 ID_2 val
# 1  0_2  1_2   2
# 2  0_4  1_4   4

table2 %>%
  semi_join(table1, by = c("ID_1", "ID_2"))
#   ID_1 ID_2 val
# 1  0_2  1_2   1
# 2  0_4  1_4   2

答案2

得分: 2

简单的示例:

table1 <- data.frame(
  "ID_1" = c("0_1", "0_2", "0_3", "0_4", "0_5"),
  "ID_2" = c("1_1", "1_2", "1_3", "1_4", "1_5"),
  val = c(1, 2, 3, 4, 5)
)

table2 <- data.frame(
  "ID_1" = c("0_2", "0_4", "0_6", "0_7", "0_8", "0_9", "0_10"),
  "ID_2" = c("1_2", "1_4", "1_6", "1_7", "1_8", "1_9", "1_10"),
  val = c(1, 2, 3, 4, 5, 6, 7)
)
使用基本的R解决方案:

ids1 <- which(interaction(table1[,c("ID_1", "ID_2")]) %in% 
               interaction(table2[,c("ID_1", "ID_2")]))
ids2 <- which(interaction(table2[,c("ID_1", "ID_2")]) %in%
                interaction(table1[,c("ID_1", "ID_2")]))
overlap1 <- table1[ids1,]
overlap2 <- table2[ids2,]
英文:

Simple repex:

table1 &lt;- data.frame(
  &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;),
  &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;),
  val = c(1, 2, 3, 4, 5)
)

table2 &lt;- data.frame(
  &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;),
  &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;),
  val = c(1, 2, 3, 4, 5, 6, 7)
)

A solution using base R:

ids1 &lt;- which(interaction(table1[,c(&quot;ID_1&quot;, &quot;ID_2&quot;)]) %in% 
               interaction(table2[,c(&quot;ID_1&quot;, &quot;ID_2&quot;)]))
ids2 &lt;- which(interaction(table2[,c(&quot;ID_1&quot;, &quot;ID_2&quot;)]) %in%
                interaction(table1[,c(&quot;ID_1&quot;, &quot;ID_2&quot;)]))
overlap1 &lt;- table1[ids1,]
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:

确定