【发布时间】:2022-01-01 02:30:36
【问题描述】:
library(dplyr)
df <- tibble(id = c("1", "2", "3", "4"),
start_date = c("2021-01-01", "2021-01-15", "2021-02-03", "2021-05-20"),
end_date = c("2021-10-11", "2021-08-17", "2021-12-20", "2021-07-01"))
df
#> # A tibble: 4 × 3
#> id start_date end_date
#> <chr> <chr> <chr>
#> 1 1 2021-01-01 2021-10-11
#> 2 2 2021-01-15 2021-08-17
#> 3 3 2021-02-03 2021-12-20
#> 4 4 2021-05-20 2021-07-01
我想得到什么?减去 end_date - start_date 并用年月数据填充行:
id start_date year_month
1 2021-01 2021-01
1 2021-01 2021-02
............................
1 2021-01 2021-10
2 2021-01 2021-01
............................
2 2021-01 2021-07
2 2021-01 2021-08
3 2021-02 2021-02
3 2021-02 2021-03
............................
3 2021-02 2021-11
3 2021-02 2021-12
4 2021-05 2021-05
4 2021-05 2021-06
4 2021-05 2021-07
如何添加行来扩展我的分组数据?
【问题讨论】:
标签: r dplyr datatable lubridate