I am trying to fill the missing date and rate. For the missing date, I wish to refill yr_month between 2019 (January) and 2021 (January). For the rate, I wish to refill any missing value as zero.
The close example I find is here - How to add only missing Dates in Dataframe but my challenge is that I have one more column by groups. It means each patient will have 2 years of data.
What's the best way I can do this on an efficient way?
# A tibble: 10 x 3
# Groups: clinic, yr_month [10]
clinic yr_month rate
<chr> <chr> <dbl>
1 patient1 2019-01 0.528
2 patient1 2019-04 0.528
3 patient1 2020-05 0.528
4 patient1 2021-01 1.06
5 patient2 2019-01 0.0671
6 patient2 2019-02 0.436
7 patient2 2019-03 0.805
8 patient2 2019-04 0.671
9 patient2 2019-05 0.268
10 patient2 2019-06 0.101
dput
structure(list(clinic = c("patient1", "patient1", "patient1",
"patient1", "patient2", "patient2", "patient2", "patient2", "patient2",
"patient2"), yr_month = c("2019-01", "2019-04", "2020-05", "2021-01",
"2019-01", "2019-02", "2019-03", "2019-04", "2019-05", "2019-06"
), rate = c(0.527704485488127, 0.527704485488127, 0.527704485488127,
1.05540897097625, 0.0671163461861136, 0.436256250209739, 0.805396154233364,
0.671163461861136, 0.268465384744455, 0.10067451927917)), row.names = c(NA,
-10L), groups = structure(list(clinic = c("patient1", "patient1",
"patient1", "patient1", "patient2", "patient2", "patient2", "patient2",
"patient2", "patient2"), yr_month = c("2019-01", "2019-04", "2020-05",
"2021-01", "2019-01", "2019-02", "2019-03", "2019-04", "2019-05",
"2019-06"), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 10L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
Expected output:
# Groups: clinic, yr_month
clinic yr_month rate
<chr> <chr> <dbl>
1 patient1 2019-01 0
1 patient1 2019-02 0
1 patient1 2019-03 0
1 patient1 2019-04 0.528
1 patient1 2019-05 0
1 patient1 2019-06 0
1 patient1 2019-07 0
1 patient1 2019-08 0
1 patient1 2019-09 0
...
25 patient2 2019-01 0.0671
26 patient2 2019-02 0.436
27 patient2 2019-03 0.805
28 patient2 2019-04 0.671
29 patient2 2019-05 0.268
30 patient2 2019-06 0.101
...
48 patient2 2021-01 0