Let's say I have a dataframe like the one below:
df <- data.frame(group = c("a", "a", "b"),
start = as.Date(c("2018-01-01", "2018-09-01", "2018-02-01")),
end = as.Date(c("2018-02-15", "2018-12-31", "2018-03-30")))
group start end
a 2018-01-01 2018-02-15
a 2018-09-01 2018-12-31
b 2018-02-01 2018-03-30
And I would like to get the following expected output:
output <- data.frame(group = c("a", "a", "a", "a", "a", "a", "b", "b"),
start = as.Date(c("2018-01-01", "2018-02-01", "2018-09-01",
"2018-10-01", "2018-11-01", "2018-12-01",
"2018-02-01", "2018-03-01")),
end = as.Date(c("2018-01-31", "2018-02-15", "2018-09-30",
"2018-10-31", "2018-11-30", "2018-12-31",
"2018-02-28", "2018-03-30")))
group start end
a 2018-01-01 2018-01-31
a 2018-02-01 2018-02-15
a 2018-09-01 2018-09-30
a 2018-10-01 2018-10-31
a 2018-11-01 2018-11-30
a 2018-12-01 2018-12-31
b 2018-02-01 2018-02-28
b 2018-03-01 2018-03-30
For each month within the sequence I would like to get a separate row which would be delimited by the 1) start date of the sequence if the latter > than the beginning of the month or beginning of month & 2) end date of the month if the latter > end date of the sequence or end date of the sequence.
Any ideas on how to do this?