Hello,
How do I get the number of businesses that had sales for 2 consecutive years, then 3 consecutive years, then four consecutive years?
Here's an example of what I'm working with
Year <- c(2013, 2012, 2011, 2010, 2013, 2012, 2011, 2010, 2013, 2012, 2011, 2010)
BusinessID <- c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)
Sales <- c(4, 10, 3, 0, 1, 0, 0, 10, 4, 2, 4, 12)
df <- data.frame(Year, BusinessID, Sales)
> df
Year BusinessID Sales
1 2010 1 4
2 2011 1 10
3 2012 1 3
4 2013 1 0
5 2010 2 1
6 2011 2 0
7 2012 2 0
8 2013 2 10
9 2010 3 4
10 2011 3 2
11 2012 3 4
12 2013 3 12
The optimal result would be a variable with the IDs of those businesses. Similar to what we get in a which() function.
For this example the end result should be something like:
> consecutive_years_2
NULL
> consecutive_years_3
[1] 1
> consecutive_years_4
[1] 3
Then eventually I would also want for example, "more or equal to 3 consecutive years" etc. Which would give:
> more_or_equal_to_consecutive_years_3
[1] 1 3