I'm looking to do hazard analysis but before I do that I want to clean my dataset so I have only the data from right before a "death", if you will. I'm studying countries and since countries don't "die" per say I need to basically find the point where an event occurs, coded as a '1' in an indicator column, and then generate a column that has 0s everywhere except for every time except for n-periods before my indicator column hits '1'.
For example, if my data were the first row, I would be looking to find a way to generate the second row.
number_of_years = 5
year = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
indicator = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0)
lag_column = c(0, 1, 1, 1, 1, 1, 0, 0, 0, 0) #I need to make this, the 5 years before the event occurs
Thank you!