I have a dataset where I have the variables: year, rcode(recipient code), promised aid, provided aid. What I want to find is the sum for the aid variables for each country (there are 194) for each year (2002-2012). There are multiple promises and payments each year, but I want the total. So basically, 2002, Afghanistan, $20 million promised, 18$ million provided, and so on. How do I calculate all of these sums?
            Asked
            
        
        
            Active
            
        
            Viewed 34 times
        
    -2
            
            
        - 
                    1Welcome to SO. Please review [how and what to ask](https://stackoverflow.com/help/how-to-ask) questions, and then provide a [minimal reproducible example/attempt](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), including sample data. – Maurits Evers Apr 03 '18 at 00:15
2 Answers
0
            
            
        We can use dplyr
library(dplyr)
df1 %>%
  group_by(year, rcode) %>%
  summarise_all(sum)
 
    
    
        akrun
        
- 874,273
- 37
- 540
- 662
0
            
            
        Since you provide no data, this is untested, but I believe that what you want is:
with(MyData, aggregate(promised_aid, by=list(year, rcode), FUN=sum))
with(MyData, aggregate(provided_aid, by=list(year, rcode), FUN=sum))
 
    
    
        G5W
        
- 36,531
- 10
- 47
- 80
