I have the following sample data frame:
x
date product release
2012-01-01 A 0
2012-01-02 A 0
2012-01-03 A 0
2012-01-04 A 1
2012-01-05 A 0
2012-01-06 A 0
2012-01-07 A 0
2012-01-08 A 0
2012-01-09 A 0
2012-01-10 A 0
2012-01-11 A 0
2012-01-12 A 0
2012-01-01 Z 0
2012-01-02 Z 1
2012-01-03 Z 0
2012-01-04 Z 0
2012-01-05 Z 0
2012-01-06 Z 0
2012-01-07 Z 0
I want to iterate through each row and generate a dayssince column based on how many days it's been since the release.
Few things to keep in mind:
- new product released = 1 no product released = 0
- the output needs to be unique to the date and the product
The desired output would be:
x
date product release dayssince
2012-01-01 A 0 0
2012-01-02 A 0 0
2012-01-03 A 0 0
2012-01-04 A 1 1
2012-01-05 A 0 2
2012-01-06 A 0 3
2012-01-07 A 0 4
2012-01-08 A 0 5
2012-01-09 A 0 6
2012-01-10 A 0 7
2012-01-11 A 0 8
2012-01-12 A 0 9
2012-01-01 Z 0 0
2012-01-02 Z 1 1
2012-01-03 Z 0 2
2012-01-04 Z 0 3
2012-01-05 Z 0 4
2012-01-06 Z 0 5
2012-01-07 Z 0 6
I've tried everything I could think of from ifelse statements and for loops to ddply.
The simplest way I've been able to approach the problem is to do the following conceptually:
x$dayssince <- ifelse(x$release > 0, 1, 0)
- Then check each row in dayssince.
- If dayssince == 1, then 1
- If dayssince < 1, then check row above.
- If row above is > 0 , then use value of row above + 1
- All this unique to the product.
Thank you in advance!
UPDATE/CLARIFICATION:
For the same products that release multiple times per year, I'm looking to get the number of days since the last release.
For example:
x
date product release dayssince
2012-01-01 A 0 0
2012-01-02 A 0 0
2012-01-03 A 0 0
2012-01-04 A 1 1
2012-01-05 A 0 2
2012-01-06 A 0 3
2012-01-07 A 0 4
2012-01-08 A 0 5
2012-01-09 A 0 6
2012-01-10 A 1 1
2012-01-11 A 0 2
2012-01-12 A 0 3
2012-01-13 A 0 4
2012-01-14 A 0 5
etc... Thanks for the flag @DMC