0

I am trying to break the date range into individual dates in R,

The source table is following:

Id   start_date   end_date    field1
1    01/03/2019   07/03/2019  text1
2    10/04/2019   15/04/2019  text2

I would like to get this output:

Id date        field1
1  01/03/2019  text1
1  02/03/2019  text1
1  03/03/2019  text1
1  04/03/2019  text1
1  05/03/2019  text1
1  06/03/2019  text1
1  07/04/2019  text1
2  10/04/2019  text2
2  11/04/2019  text2
2  12/04/2019  text2
2  13/04/2019  text2
2  14/04/2019  text2
2  15/04/2019  text2

1 Answers1

0

Does this work:

library(dplyr)
library(purrr)
df %>% group_by(Id, field1) %>% mutate(date = map2(lubridate::dmy(start_date), lubridate::dmy(end_date), `:`)) %>% unnest(date) %>% 
   mutate(date = as.Date(date, origin = '1970-01-01')) %>% select(-c(start_date, end_date))
# A tibble: 13 x 3
# Groups:   Id, field1 [2]
      Id field1 date      
   <dbl> <chr>  <date>    
 1     1 text1  2019-03-01
 2     1 text1  2019-03-02
 3     1 text1  2019-03-03
 4     1 text1  2019-03-04
 5     1 text1  2019-03-05
 6     1 text1  2019-03-06
 7     1 text1  2019-03-07
 8     2 text2  2019-04-10
 9     2 text2  2019-04-11
10     2 text2  2019-04-12
11     2 text2  2019-04-13
12     2 text2  2019-04-14
13     2 text2  2019-04-15
> 
Karthik S
  • 11,348
  • 2
  • 11
  • 25