I am trying to count the years a product has been on offer since recorded history.
In the example below, I'd like to start the counter at the first non-zero sales recorded.
Note that I may have zero recorded sales in the next few years, but the counter should count the years as valid.
I experimented with rleid
, but cannot figure out a way to differentiate the initial 0s vs. within-lifespan 0s.
tt <- data.table(YEAR=2007:2018,
SALES=c(0,0,0,2,3,5,1,0,9,0,3,4),
YEARS_IN=c(0,0,0,1,2,3,4,5,6,7,8,9))
> tt
YEAR SALES YEARS_IN
1: 2007 0 0
2: 2008 0 0
3: 2009 0 0
4: 2010 2 1
5: 2011 3 2
6: 2012 5 3
7: 2013 1 4
8: 2014 0 5
9: 2015 9 6
10: 2016 0 7
11: 2017 3 8
12: 2018 4 9
Here's a one line solution using base R -
tt <- data.table::data.table(YEAR=2007:2018,
SALES=c(0,0,0,2,3,5,1,0,9,0,3,4),
YEARS_IN=c(0,0,0,1,2,3,4,5,6,7,8,9))
tt$Calc_Years <- cumsum(cumsum(tt$SALES) > 0)
tt
YEAR SALES YEARS_IN Calc_Years
1: 2007 0 0 0
2: 2008 0 0 0
3: 2009 0 0 0
4: 2010 2 1 1
5: 2011 3 2 2
6: 2012 5 3 3
7: 2013 1 4 4
8: 2014 0 5 5
9: 2015 9 6 6
10: 2016 0 7 7
11: 2017 3 8 8
12: 2018 4 9 9
Thanks to Simon, here's a data.table
version -
tt[ , Calc_Years := cumsum(cumsum(SALES) > 0)]
library(data.table)
tt <- data.table(YEAR=2007:2018,
SALES=c(0,0,0,2,3,5,1,0,9,0,3,4),
YEARS_IN=c(0,0,0,1,2,3,4,5,6,7,8,9))
temp <- min(which(tt[,SALES]!=0))
tt[-c(1:(temp-1)),Comp:=.I]
tt[c(1:(temp-1)),Comp:=0]
tt
YEAR SALES YEARS_IN Comp
1: 2007 0 0 0
2: 2008 0 0 0
3: 2009 0 0 0
4: 2010 2 1 1
5: 2011 3 2 2
6: 2012 5 3 3
7: 2013 1 4 4
8: 2014 0 5 5
9: 2015 9 6 6
10: 2016 0 7 7
11: 2017 3 8 8
12: 2018 4 9 9
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.