Search code examples
rtime-seriesintervals

Split data in intervals of pre defined lenth, R


I have a data set on the form below:

Id START DATE END DATE
1 2020-01-01 2021-01-01
2 2000-03-02 2021-01-01

I would like to split in 90 days interval from start to end date. I might be able to find a work around, but since I need to perform it multiple times, i am looking for a relatively simple code. I am thinking do.call might be the way to go?

The last interval can be less than 90 days in case the ending interval does not fit with a 90 day period.

Id START DATE END DATE
1 2020-01-01 2020-03-31
1 2020-03-31 2020-06-29
...
2 2000-03-02 2000-05-31

I got some very good suggestions for solutions, unfortunate i do not have access to the updated version of dplyr, and thus not the reframe()function.

I found a solution but any other solutions is still very welcome


Solution

  • I have found a solution, however I dont think it is a very pretty one:

    Assume dt is my data table, the following whileloop will solve the problem using the data.table package. Before running the code i check that all end dates fall after start date.

    continue <- TRUE
    dt[,add_row:=end-start>90]
    while(continu==TRUE{
    dt1 <- dt[add_row==TRUE]
    dt2 <- dt[add_row==TRUE]
    dt3 <- dt[add_row==FALSE]
    
    # dt1 updated with new enddate
    d1[,end:=start+90]
    #dt2 updated with new start
    dt2[,start:=start+90]
    #dt3 contains individuals not to be updated
    dt <- rbind[dt1,dt2,dt2]
    
    #check if loop should continue
    dt[,add_row:=end-start>90]
    continue <- any(tmp[,add_row])
    }