Arani
Arani

Reputation: 833

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

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

Upvotes: 0

Views: 47

Answers (1)

mt1022
mt1022

Reputation: 17309

The basic idea is to combine the two dataset, group by x and summarize y with sum and there are a couple of ways to do it:

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

base R aggregate:

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

dplyr:

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

The data:

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')

Upvotes: 3

Related Questions