Add data from a data table to another using values of a column

时间:2017-08-05 12:30:59

标签: r dataframe data.table dplyr apply

I know the question is confusing, but I hope the example will make it simple.

I have two tables:

x    y
1    23
2    34
3    76
4    31

&

x    y
1    78
3    51
5    54

I need to add the y columns based on x values. I can do it using loops, but don't want to. It will be better if the solution uses base, dplyr, data.table functions as I am most familiar with those, I am okay with apply family of functions as well. The output should look like this:

x    y
1    101
2    34
3    127
4    31
5    54

1 个答案:

答案 0 :(得分:3)

基本思想是将两个数据集组合在一起,按x进行分组,并将ysum进行汇总,并且有几种方法可以执行此操作:

data.table

rbind(dtt1, dtt2)[, .(y = sum(y)), by = x]

#    x   y
# 1: 1 101
# 2: 2  34
# 3: 3 127
# 4: 4  31
# 5: 5  54

基础R aggregate

aggregate(y ~ x, rbind(dtt1, dtt2), FUN = sum)

dplyr

rbind(dtt1, dtt2) %>% group_by(x) %>% summarize(y = sum(y))

数据:

library(data.table)

dtt1 <- fread('x    y
1    23
2    34
3    76
4    31')

dtt2 <- fread('x    y
1    78
3    51
5    54')