I am quite new to R, so apologies in advance if I state something wrong :)
I have a dataframe consisting of 395 rows and 4973 columns, sorted by months, with number of occurrence per month (ranges from 0 to eg 25) for a lot of companies. The number of occurrence was summarised from daily data grouped by month and year. My dataframe df looks something like that (only a few months and 3 companies):
Date FirmA FirmB FirmC
01-2015 20 NA 20
02-2015 21 2 1
03-2015 22 3 2
04-2015 24 7 5
05-2015 10 10 10
06-2015 9 20 2
07-2015 13 22 1
08-2015 20 19 1
I have now the task to sum up the occurences per company by a three month rolling window from months t-3 to t-1 (the 3 previous months). However, the sum should have following conditions. It should have at least 10 occurrences during the three month window and at least 3 occurrences in month t-1. It doesn't matter if an NA is in t-3 and/or t-2, as long as the two conditions are met.
It should look like that.
Date FirmA FirmB FirmC
01-2015 NA NA NA
02-2015 20 NA 20
03-2015 41 NA NA
04-2015 63 NA NA
05-2015 67 12 NA
06-2015 56 20 17
07-2015 43 37 NA
08-2015 32 52 NA
I have no clue, how to approach that, especially the combination of rolling window/sum (probably something with lag) and the conditions regarding which numbers to use and which not.