I would like to create a column equal to a running sum of data$Rating, given two conditions are true in column 3 and 4, specifically that data$Year< current year and data$ID is equal to current ID.
In words this should calculate the cumulative sum of ratings for each id up until the previous year. And it should do this for each row in the data frame (about 50,000 rows). Given the size of the data frame, I'd prefer not to loop, if this at all possible.
I've provided a short example of how this would look below...
> head(data[,c(3,4,13)])
Year ID Rating CumSum
1 2010 13578 2 0
2 2010 13579 1 0
3 2010 13575 3 0
4 2011 13575 4 3
5 2012 13578 3 2
6 2012 13579 2 1
7 2012 13579 4 1
I'm coming from a spreadsheet background, so am still thinking in terms of SUMIFS, etc. (which would nicely solve my problem in Excel), so apologies if the language isn't precise.