0

df1

acc_id || s1_2005 || s1_2006
87         1       1

df2

acc_id || s2_2005 || s2_2006
87            2       3

output

df3

acc_id ||s1_2005 + s2_2005 || s1_2006 + s2_2006
87                3                   4
manotheshark
  • 4,199
  • 16
  • 30
  • 1
    In order for us to help you, please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For example, to produce a minimal data set, you can use `head()`, `subset()`, or the indices. Then use `dput()` to give us something that can be put in R immediately. Also, please make sure you know what to do [when someone answers your question](https://stackoverflow.com/help/someone-answers). More info can be found at Stack Overflow's [help center](https://stackoverflow.com/help). Thank you! – iamericfletcher Nov 16 '20 at 16:35
  • Thank you for the information . I will keep this in mind . – abhishek hiremath Nov 16 '20 at 17:42

2 Answers2

0

There are a a bunch of ways to do this. One way is to use rbind() to stack the data together into one dataframe, and then to aggregate() the data to the acc_id level.

# example data
df1 <- data.frame(
    acc_id = c(87, 88),
    s1_2005 = c(1, 2),
    s1_2006 = c(1, 2))

df2 <- data.frame(
    acc_id = c(87, 89),
    s1_2005 = c(2, 4),
    s1_2006 = c(3, 4))

# combine data by stacking
df <- rbind(df1, df2)

# sum s1_2005 and s1_2006 by acc_id
res <- aggregate(df[ , c("s1_2005", "s1_2006")], by=df["acc_id"], FUN=sum)

# print result
res

The result:

  acc_id s1_2005 s1_2006
      87       3       4
      88       2       2
      89       4       4
DanY
  • 5,699
  • 1
  • 12
  • 32
  • Thank you for your response , but i need to add s1_2005 and s2_2005 for each account_id. – abhishek hiremath Nov 16 '20 at 17:42
  • That is *exactly* what my code does. I used 3 account IDs (87, 88, and 89) so you could see how the data for ID 87 were being added together. – DanY Nov 16 '20 at 18:36
0

Using dplyr

library(dplyr)
bind_rows(df1, df2) %>% 
    group_by(acc_id) %>% 
    summarise(across(everything(), sum), .groups = 'drop')

-output

# A tibble: 3 x 3
#  acc_id s1_2005 s1_2006
#   <dbl>   <dbl>   <dbl>
#1     87       3       4
#2     88       2       2
#3     89       4       4

data

df1 <- data.frame(
    acc_id = c(87, 88),
    s1_2005 = c(1, 2),
    s1_2006 = c(1, 2))

df2 <- data.frame(
    acc_id = c(87, 89),
    s1_2005 = c(2, 4),
    s1_2006 = c(3, 4))
akrun
  • 789,025
  • 32
  • 460
  • 575