Sum values of two different datasets but following the same date in R

Advertisements

I have two datasets called ‘ro’ and ‘rt’ with different lenght, in each dataset we have a column called ‘price’ and a col called ‘date’. I want to sum the prices following the same date.
so i would like to create a new dataset in which, for example in date 6/1/22 (which is in both dataset) in ‘ro’ there’s 20$ and in ‘rt’ there’s 40$. the new dataset will have another column with the date (6/1/22) and another column with 60$ (wich is the sum)

of course if there’s not the same date, there won’t be any sum; (in ‘ro’ we have date 3/5/22 with 90$, but there’s not the same date ‘rt’, in the new dataset will be simply the same row, without any sum)


dataset 'ro' 

   Date           A                           

1  2015-01-17     2  
2  2015-01-18     7   
3  2015-01-19     1       
4  2015-01-11     8      


dataset 'rt'

   Date           A       

1  2015-01-17     1      
2  2015-01-10     2   
3  2015-01-19     1       
4  2015-01-11     1   
5  2015-02-12     5
6  2015-04-9      2


new dataset       
                  A

1  2015-01-17     3      
2  2015-01-10     2   
3  2015-01-19     2       
4  2015-01-11     9   
5  2015-01-18     7
6  2015-02-12     5
7  2015-04-9      2

this is what i would like :,)

>Solution :

We could bind the datasets and do a group by sum

library(dplyr) #version >= 1.1.0
bind_rows(ro, rt) %>% 
   reframe(A = sum(A), .by = Date)

-output

        Date A
1 2015-01-17 3
2 2015-01-18 7
3 2015-01-19 2
4 2015-01-11 9
5 2015-01-10 2
6 2015-02-12 5
7  2015-04-9 2

Leave a ReplyCancel reply