I have a dataframe that looks like this:
ID Group Start Date End Date
1 A 2018-08-08 2018-08-09
1 A 2018-08-07 2018-08-08
1 A 2018-08-05 2018-08-07
1 B 2018-08-08 2018-08-09
1 B 2018-08-07 2018-08-08
2 A 2018-08-08 2018-08-09
2 A 2018-08-07 2018-08-08
2 A 2018-08-01 2018-08-07
2 B 2018-08-08 2018-08-09
2 B 2018-08-07 2018-08-08
3 B 2018-08-07 2018-08-08
4 B 2018-08-07 2018-08-08
4 B 2018-08-01 2018-08-07
And I'd like collapse it so that if the Start Date
variable in one row matches the End Date
variable in the next row, the rows are combined while grouping by the ID
and Group
variables:
ID Group Start Date End Date
1 A 2018-08-05 2018-08-09
1 B 2018-08-07 2018-08-09
2 A 2018-08-01 2018-08-09
2 B 2018-08-07 2018-08-09
3 B 2018-08-07 2018-08-08
4 B 2018-08-01 2018-08-08
In dplyr, I know it's possible to do this through something similar to the following:
df %>%
group_by(ID, Group) %>%
rowwise() %>%
do(somefunction(x){})
But I need help in writing the function. Or if there's a for loop or other implementation that could accomplish this, it would be much appreciated.
Edit I've modified the example data for more clarity.