I have a dataframe df1 with information on the number of acquisitions that a company has made during a certain year. I would need to
a) construct a dummy variable if there are observations available three consecutive preceding years for each company each year
b) if there are three consecutive preceding years for that company-year, then sum the number of acquisitions made during that three-year period
df1 <- data.frame(ID=c('XXXX-1999','XXXX-2000', 'XXXX-2001', 'YYYY-1999',
'YYYY-2000', 'ZZZZ-1999','ZZZZ-2000','ZZZZ-2001', 'ZZZZ-2002'),
No.of.Transactions=c(1,0,2,2,2,4,1,0,3))
where ID is the observation for a company during a year. The desired output is below
# Desired output
# ID | No.of.Transactions | 3 preceding yrs available dummy? |
No.of.Transactions during 3 preceding yrs
# XXXX-1999 1 0 N/A
# XXXX-2000 0 0 N/A
# XXXX-2001 2 1 3
# YYYY-1999 2 0 N/A
# YYYY-2000 2 0 N/A
# ZZZZ-1999 4 0 N/A
# ZZZZ-2000 1 0 N/A
# ZZZZ-2001 0 1 5
# ZZZZ-2002 3 1 4
So if the "3 preceding yrs available dummy?" column takes a value of 1, then the final column should sum up all the transactions for the company during the focal and two preceding years.
Thank you in advance!