I've got a dataset that tracks the revenue of millions of companies over many years. The data looks like the simplified version below:
dat <- data.frame(Company = c("a","b","c","d","e","f"), rev_2001 = c(NA, 20, 10, NA, NA, 10),
rev_2002 = c(10, 50, 20, 30, NA, 0), rev_2003 = c(20, NA, 0, NA, NA, 30), rev_2004 = c(NA, 60, 0, 50, NA, 50), rev_2005 = c(NA, 30, NA, 0, NA, 60))
I wanted to create a variable (a new column that we could label 'closure.year') that would capture the last year that the company had a revenue different from 0 or NA. I'm struggling with two things:
- The fact that I want to ignore both zeros and NAs, but I don't want to convert NAs into zeros nor the other way around;
- As you can see, the data has some zeros and NAs not only in the last years observed, but also in some intermediate years. If the company had a revenue after a year with a 0 or NA, that year would not be considered as a closure.year. Also, if the company had not stopped receiving revenues, the variable closure.year would be NA.
In sum, I would like to have final data that would look like:
Thank you so much!
