具有公共ID的rbind数据帧行

具有公共ID的rbind数据帧行,r,R,我需要制作一个由原始数据帧的所有列组成的数据帧,它们已经由相同的列组成,其中两个原始数据帧都有一些共同的ID。在本例中,df1和df2都有ID为1的行,因此结果数据帧中有两个数据帧中ID为1的所有行。首先使用intersect获得公共ID,然后使用subset和rbind两个数据帧- ID A B C 1 0 0 0 1 1 0 1 1 49 49 32 这里有一个带tidyverse的选项 你试过什么r码?把它编辑到你的问题中

我需要制作一个由原始数据帧的所有列组成的数据帧,它们已经由相同的列组成,其中两个原始数据帧都有一些共同的ID。在本例中,df1和df2都有ID为1的行,因此结果数据帧中有两个数据帧中ID为1的所有行。

首先使用intersect获得公共ID,然后使用subset和rbind两个数据帧-

ID   A    B    C
1    0    0    0
1    1    0    1
1    49   49   32
这里有一个带tidyverse的选项


你试过什么r码?把它编辑到你的问题中嘿,内特,我不知道怎么做,所以没怎么试过-我不相信合并对我有用,但这是我想到的唯一一件事,你的意思是要选择具有给定ID的行,然后将它们绑定在一起吗?OP说这是基于ID的交集columns@rdk下次发布数据时使用read.table格式,避免其他人搜索输入数据
ID   A    B    C
1    0    0    0
1    1    0    1
1    49   49   32
valid_ids <- intersect(df1$ID, df2$ID)

rbind(df1[df1$ID %in% valid_ids, ], df2[df2$ID %in% valid_ids, ])

  ID  A  B  C
1  1  0  0  0
2  1  1  0  1
3  1 49 49 32
library(tidyverse)
list(df1, df2) %>% 
    bind_rows(.id = 'grp') %>% 
    group_by(ID) %>% 
    filter(n_distinct(grp) > 1) %>% 
    select(-grp)
# A tibble: 3 x 4
# Groups:   ID [1]
#     ID     A     B     C
#  <int> <int> <int> <int>
#1     1     0     0     0
#2     1     1     0     1
#3     1    49    49    32