Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Summing two dataframes based on common value

Tags:

r

I have a dataframe that looks like

    day.of.week count
1           0     3
2           3     1
3           4     1
4           5     1
5           6     3

and another like

    day.of.week count
1           0    17
2           1     6
3           2     1
4           3     1
5           4     5
6           5     1
7           6    13

I want to add the values from df1 to df2 based on day.of.week. I was trying to use ddply

total=ddply(merge(total, subtotal, all.x=TRUE,all.y=TRUE),
                   .(day.of.week), summarize, count=sum(count))

which almost works, but merge combines rows that have a shared value. For instance in the example above for day.of.week=5. Rather than being merged to two records each with count one, it is instead merged to one record of count one, so instead of total count of two I get a total count of one.

       day.of.week count
  1            0     3
  2            0    17
  3            1     6
  4            2     1
  5            3     1
  6            4     1
  7            4     5
  8            5     1
  9            6     3
  10           6    13
like image 263
matt_k Avatar asked Feb 23 '23 20:02

matt_k


1 Answers

There is no need to merge. You can simply do

ddply(rbind(d1, d2), .(day.of.week), summarize, sum_count = sum(count))

I have assumed that both data frames have identical column names day.of.week and count

like image 143
Ramnath Avatar answered Feb 25 '23 12:02

Ramnath