1

I have this dataframe:

DF <- tribble(
  ~date, ~value,
  "2021-12-01", 1,
  "2022-01-01", 2)

  date       value
  <chr>      <dbl>
1 2021-12-01     1
2 2022-01-01     2

Usually I use complete to fill each month with all days: Here I use again complete:

library(tidyr)
library(dplyr)
library(lubridate)
DF %>% 
  mutate(date = ymd(date)) %>% 
  complete(date = seq(min(date), max(date), by = 'day')) %>% 
  data.frame()

And I get:

         date value
1  2021-12-01     1
2  2021-12-02    NA
3  2021-12-03    NA
4  2021-12-04    NA
5  2021-12-05    NA
6  2021-12-06    NA
7  2021-12-07    NA
8  2021-12-08    NA
9  2021-12-09    NA
10 2021-12-10    NA
11 2021-12-11    NA
12 2021-12-12    NA
13 2021-12-13    NA
14 2021-12-14    NA
15 2021-12-15    NA
16 2021-12-16    NA
17 2021-12-17    NA
18 2021-12-18    NA
19 2021-12-19    NA
20 2021-12-20    NA
21 2021-12-21    NA
22 2021-12-22    NA
23 2021-12-23    NA
24 2021-12-24    NA
25 2021-12-25    NA
26 2021-12-26    NA
27 2021-12-27    NA
28 2021-12-28    NA
29 2021-12-29    NA
30 2021-12-30    NA
31 2021-12-31    NA
32 2022-01-01     2

So January 2022 is not filled or completed?!

The desired output would be the same above + all days of january 2022.

I am looking for a solution without adding any additional date to the dataframe.

TarJae
  • 72,363
  • 6
  • 19
  • 66

1 Answers1

1

If you want to include January 2022, you need to specify the last day of January in the DF:

DF <- tribble(
  ~date, ~value,
  "2021-12-01", 1,
  "2022-01-01", 2)

DF %>% 
  mutate(date = ymd(date)) %>% 
  complete(date = seq(min(date), max(ceiling_date(date, unit = "month") - ddays(1)), by = 'day')) %>% 
  data.frame()
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
hyman
  • 315
  • 3
  • 13
  • Thank you for your reply. I am looking for a solution without adding anything to the dataframe. I will clarify this! – TarJae Jan 01 '22 at 15:33
  • 1
    ok, i adapted the solution – hyman Jan 01 '22 at 15:41
  • This looks very good. To get it only for both months just add `group_by(month(date)` to your code and some explanation then it is perfect. :-). – TarJae Jan 01 '22 at 15:51