Trying to generate a date sequence in R programming (using lubridate) of dates with a given start date and frequency is not a numeric value but days on which the dates can occur.
Given is the below table where group, start date, day and occurrence flag is defined
+-------+------------+-----+-----+
| Group | start_date | Day | Y/N |
+-------+------------+-----+-----+
| foo | 02-06-2021 | Mon | 0 |
| foo | 02-06-2021 | Tue | 1 |
| foo | 02-06-2021 | Wed | 0 |
| foo | 02-06-2021 | Thu | 1 |
| foo | 02-06-2021 | Fri | 1 |
| foo | 02-06-2021 | Sat | 1 |
| foo | 02-06-2021 | Sun | 0 |
| bar | 02-06-2021 | Mon | 1 |
| bar | 02-06-2021 | Tue | 0 |
| bar | 02-06-2021 | Wed | 0 |
| bar | 02-06-2021 | Thu | 1 |
| bar | 02-06-2021 | Fri | 1 |
| bar | 02-06-2021 | Sat | 0 |
| bar | 02-06-2021 | Sun | 0 |
+-------+------------+-----+-----+
Required output is as below.
+-------+------------+---------------------+
| Group | given_date | next_available_date |
+-------+------------+---------------------+
| foo | 02-06-2021 | 03-06-2021 |
| foo | 04-06-2021 | 04-06-2021 |
| foo | 06-06-2021 | 08-06-2021 |
| bar | 02-06-2021 | 03-06-2021 |
| bar | 05-06-2021 | 07-06-2021 |
+-------+------------+---------------------+
Some ideas around while loop which i thought could be tired.
for each given_date{
inputdate = given_date
while(true){
{
if(group =="Foo" & day(inputdate) in ('Tue','Thu','Fri','Sat')
next_available_date=inputdate
break
}
else
{
inputdate = inputdate+(1 day) (repeat the loop until if condition is satisfied)
}
}
}
If condition for different groups could be different.
Not able to figure how to leverage uneven frequency to get the next available date.