如何根据R中匹配的ID添加列值?

时间:2022-05-28 14:59:29

I have two data frames:

我有两个数据框:

A:

ID    Var1    Var2    Var3
1       0       3       4
2       1       5       0
3       1       6       7

B:

ID    Var1    Var2    Var3    
1       2       4       2
2       2       1       1
3       0       2       1
4       1       0       3

I want to add the columns from A and B based on matching ID's to get data frame C, and keep row 4 from B (even though it does not have a matching ID from A):

我想根据匹配ID添加A和B中的列以获取数据帧C,并从B保留第4行(即使它没有来自A的匹配ID):

ID    Var1    Var2    Var3
1       2       7       6
2       3       6       1
3       1       8       8
4       1       0       3

1 个解决方案

#1


2  

rbind and aggregate by ID:

rbind和ID汇总:

aggregate(. ~ ID, data=rbind(A,B), sum)

#  ID Var1 Var2 Var3
#1  1    2    7    6
#2  2    3    6    1
#3  3    1    8    8
#4  4    1    0    3

In data.table you can similarly do:

在data.table中,您可以类似地执行:

library(data.table)
setDT(rbind(A,B))[, lapply(.SD, sum), by=ID]

And there would be analogous solutions in dplyr and sql or whatever else. Bind the rows, group by ID, sum.

在dplyr和sql或其他任何东西中都会有类似的解决方案。绑定行,按ID分组,总和。

#1


2  

rbind and aggregate by ID:

rbind和ID汇总:

aggregate(. ~ ID, data=rbind(A,B), sum)

#  ID Var1 Var2 Var3
#1  1    2    7    6
#2  2    3    6    1
#3  3    1    8    8
#4  4    1    0    3

In data.table you can similarly do:

在data.table中,您可以类似地执行:

library(data.table)
setDT(rbind(A,B))[, lapply(.SD, sum), by=ID]

And there would be analogous solutions in dplyr and sql or whatever else. Bind the rows, group by ID, sum.

在dplyr和sql或其他任何东西中都会有类似的解决方案。绑定行,按ID分组,总和。