-2

I am trying to convert below data on daily basis based on range available in start_date & end_date_ column.

enter image description here

to this output (sum):

enter image description here

Phil
  • 7,287
  • 3
  • 36
  • 66
gray
  • 17
  • 2

1 Answers1

1

Please use dput() when posting data frames next time!

Example data

# A tibble: 4 × 4
     id start      end        inventory
  <int> <chr>      <chr>          <dbl>
1     1 01/05/2022 02/05/2022       100
2     2 10/05/2022 15/05/2022        50
3     3 11/05/2022 21/05/2022        80
4     4 14/05/2022 17/05/2022        10

Transform the data

df %>% 
  mutate(across(2:3, ~ as.Date(.x, 
                                format = "%d/%m/%Y"))) %>% 
  pivot_longer(cols = c(start, end), values_to = "date") %>% 
  arrange(date) %>% 
  select(date, inventory) 

# A tibble: 8 × 2
  date       inventory
  <date>         <dbl>
1 2022-05-01       100
2 2022-05-02       100
3 2022-05-10        50
4 2022-05-11        80
5 2022-05-14        10
6 2022-05-15        50
7 2022-05-17        10
8 2022-05-21        80

Expand the dates and left_join

left_join(tibble(date = seq(first(df$date), 
                            last(df$date), 
                            by = "day")), df)

# A tibble: 21 × 2
   date       inventory
   <date>         <dbl>
 1 2022-05-01       100
 2 2022-05-02       100
 3 2022-05-03        NA
 4 2022-05-04        NA
 5 2022-05-05        NA
 6 2022-05-06        NA
 7 2022-05-07        NA
 8 2022-05-08        NA
 9 2022-05-09        NA
10 2022-05-10        50
# … with 11 more rows
Chamkrai
  • 5,912
  • 1
  • 4
  • 14