I have the following data frame,
id, date, state
1 2012-01-01 a
1 2012-01-02 a
1 2012-01-03 a
1 2012-01-04 b
1 2012-01-05 b
2 2013-01-01 a
2 2013-01-02 a
2 2013-01-03 b
2 2013-01-04 b
For each id, I want to find the date when the state changed from a to b following which I want it inserted as a column for that id. So the above example would yield
id, date, state, changedate
1 2012-01-01 a 2012-01-03
1 2012-01-02 a 2012-01-03
1 2012-01-03 a 2012-01-03
1 2012-01-04 b 2012-01-03
1 2012-01-05 b 2012-01-03
2 2013-01-01 a 2013-01-02
2 2013-01-02 a 2013-01-02
2 2013-01-03 b 2013-01-02
2 2013-01-04 b 2013-01-02
Is there a way to do this elegantly through plyr functions or even in base R?
Thanks in advance.
Edit: As Sebastian mentions, I assume that the data.frame is ordered by the column
date.One of the many solutions. Probably the tricky bit is to find the transition period. This can be accomplished with the help of
rle.Alternative solution using
data.table(I just noticed that you also have a.id.column with which we can split and apply the date with the transition index found viarle).