I have date ranges that are grouped by two variables (id
and type
) that are currently stored in a data frame called data
. My goal is to ex
Here is one way to perform such a transformation using base functions
do.call(rbind,Map(function(id,type,from,to) {
dts <- seq(from=from, to=to, by="1 day")
dur <- length(dts)
data.frame(
id=rep(id, dur),
type=rep(type,dur),
date=dts
)
}, data$id, data$type, data$from, data$to))
And the first chunck of the output is
id type date
1 1 a 2009-02-21 02:00:00
2 1 a 2009-02-22 02:00:00
3 1 a 2009-02-23 02:00:00
4 1 a 2009-02-25 02:00:00
5 1 b 2009-02-25 02:00:00
6 1 b 2009-02-26 02:00:00
7 1 c 2009-02-26 02:00:00
8 1 c 2009-02-27 02:00:00
9 1 c 2009-02-28 02:00:00
10 1 c 2009-03-01 02:00:00
11 1 b 2009-02-26 02:00:00