I have a very large dataframe(df) with date,id, x columns.
The date column is formatted as date (y.m.d) and has value for some days scatteredly.Each group in the id column has a different start and end date.
tl;dr my goal is to fill the missing date for each group by adding a row with an empty value for x column. df
| date | id | X |
|---|---|---|
| 22.02.02 | A | 26 |
| 22.02.06 | A | 45 |
| 22.02.08 | A | 46 |
| 22.02.05 | B | 37 |
| 22.02.09 | B | 82 |
| 22.02.12 | B | 56 |
| 22.01.13 | C | 61 |
| 22.01.17 | C | 90 |
| 22.01.20 | C | 43 |
| 22.01.23 | C | 12 |
my desired output df:
| date | id | X |
|---|---|---|
| 22.02.02 | A | 26 |
| 22.02.03 | A | |
| 22.02.04 | A | |
| 22.02.05 | A | |
| 22.02.06 | A | 45 |
| 22.02.07 | A | |
| 22.02.08 | A | 46 |
| 22.02.05 | B | 37 |
| 22.02.06 | B | |
| 22.02.07 | B | |
| 22.02.08 | B | |
| 22.02.09 | B | 82 |
| 22.02.10 | B | |
| 22.02.11 | B | |
| 22.02.12 | B | 56 |
| 22.01.13 | C | 61 |
| 22.01.14 | C | |
| 22.01.15 | C | |
| 22.01.16 | C | |
| 22.01.17 | C | 90 |
| 22.01.18 | C | |
| 22.01.19 | C | |
| 22.01.20 | C | 43 |
| 22.01.21 | C | |
| 22.01.22 | C | |
| 22.01.23 | C | 12 |
I do not know how to solve this kind of questions, what is the most efficient way to solve this?!
Here my code which does not give me the result:
df_output <- df %>%
group_by(id) %>%
complete(df$date = seq(min(df$date), max(df$date), by =
"1 day"), fill = list(number = 0))
I am new to R and using dplyr.