Expand time interval data into days in R -
suppose have data looks this:
interval_id indiv_id role start_date end_date 1 1 2006-05-01 2006-06-16 2 1 b 2006-06-16 2006-10-16 3 1 2006-10-16 2009-10-16 4 1 b 2009-10-16 2014-04-23 5 2 2007-12-15 2009-10-16 6 2 b 2009-10-16 2011-07-01
but want data looks (taking first role interval example):
interval_id indiv_id role day 1 1 2006-05-01 1 1 2006-05-02 1 1 2006-05-03 1 1 2006-05-04 1 1 2006-05-05 1 1 2006-05-06 ... ... ... ... 1 1 2006-06-16
i'm doing loop in r. pretty sure unnecessary. there package expanding time intervals this? seems reshape-type job since i'm kind of turning time interval long format data set.
thanks.
here's way plyr
(assuming, once again, data in df
):
library(plyr) byday = ddply(df, .(interval_id, indiv_id, role), transform, day=seq(as.date(start_date), as.date(end_date), by=1))
the start_date , end_date values repeated in every row, can remove if wish.
Comments
Post a Comment