I am hoping to find a data.table + lubridate solution to the following problem.
Suppose that I have the following dataset:
library(data.table)
library(lubridate)
library(magrittr)
sample <- data.table(start = c("2018-12-22 23:00:00",
"2018-12-23 06:00:00",
"2018-12-22 06:00:00",
"2018-12-23 06:00:00"),
end = c("2018-12-23 06:00:00",
"2018-12-23 13:00:00",
"2018-12-23 12:00:00",
"2018-12-24 01:00:00"),
store = c("A", "A", "B", "B"),
var = 1:4)
sample[, start:= ymd_hms(start)]
sample[, end := ymd_hms(end)]
The output looks like:
> sample
start end store var
1: 2018-12-22 23:00:00 2018-12-23 06:00:00 A 1
2: 2018-12-23 06:00:00 2018-12-23 13:00:00 A 2
3: 2018-12-22 06:00:00 2018-12-23 12:00:00 B 3
4: 2018-12-23 06:00:00 2018-12-24 01:00:00 B 4
Notice that on rows 1, 3, and 4, we have observations that span multiple dates. I would like to break up these observations so that they only take up one calendar date each. It is also possible that a start date and end date spans multiple days, but I would like to have one observation for each date. For the sample above, the data.table should look like:
start end store var
1: 2018-12-22 23:00:00 2018-12-22 23:59:59 A 1
2: 2018-12-23 00:00:00 2018-12-23 06:00:00 A 1
3: 2018-12-23 06:00:00 2018-12-23 13:00:00 A 2
4: 2018-12-22 06:00:00 2018-12-22 23:59:59 B 3
5: 2018-12-23 00:00:00 2018-12-23 12:00:00 B 3
6: 2018-12-23 06:00:00 2018-12-23 23:59:59 B 4
7: 2018-12-24 00:00:00 2018-12-24 01:00:00 B 4
It is important the var variable is the same when we break up the observation across multiple dates.
Thanks!