I have a dataframe with more than 500 rows with columns as below. There are more than 5 groups, 11 rows for each group where each value corresponds to a different month in the year.
Date Group
1 2019-07-06 1
2 2019-08-06 1
3 2019-09-06 1
4 2019-09-06 1
5 2019-10-06 1
6 2019-08-06 2
7 2019-09-06 2
8 2019-09-06 2
9 2019-10-06 2
The problem is that there is repetition of 2019-09-06
, otherwise it should have been January-November. I want to change the second 2019-09-06 to 2019-10-06 and last row for each group to 2019-11-06.
I thought of using case_when
, but it would change both values where month is 09
.
Can someone please help me how to do this?
CodePudding user response:
A possible solution:
library(dplyr)
df %>%
group_by(Group) %>%
mutate(Date = seq(as.Date(first(Date)), length = n(), by="1 month")) %>%
ungroup
#> # A tibble: 9 × 2
#> Date Group
#> <date> <int>
#> 1 2019-07-06 1
#> 2 2019-08-06 1
#> 3 2019-09-06 1
#> 4 2019-10-06 1
#> 5 2019-11-06 1
#> 6 2019-08-06 2
#> 7 2019-09-06 2
#> 8 2019-10-06 2
#> 9 2019-11-06 2