I have this data frame below.
my_tibble <- tibble(
Week_Start_Day = mdy(c("06-06-2022","06-20-2022","07-04-2022")),
Duration = c(8,1,3),
Weekly_Hours = c(10,5.5,15.667),
Project = c("ClientA","ClientB","ClientC")
)
You interpret it as follows:
- Each project has a start day (Week_Start_Day) — the day work begins
- Each project has a Duration in weeks
- Each project demands x hours per Week (Weekly_Hours)
- Each project has a name (Project_Name)
I want to create a separate row for each week of the project so I can visualize it over a weekly view. For example, row 1 has a Duration of 8, meaning the project goes for 8 weeks. Thus, it should be duplicated 7 times. Each duplicated row should have a new Week_Start_Day, which should be 7 more than the prior. So for ClientA we should have a total of 8 rows across 6/6, 6/13, 6/20, 6/27, etc. All other data needs to remain the same.
I have tried numerous loops and all have failed. I attempted manually adding one row just to see if I'd get some inspiration, but I am unable to convert this into a loop. Here is what one instance of adding a new row should look like:
my_tibble[4,]$Week_Start_Day <- my_tibble[1,]$Week_Start_Day + 7
my_tibble[4,]$Duration <- my_tibble[1,]$Duration
my_tibble[4,]$Weekly_Hours <- my_tibble[1,]$Weekly_Hours
my_tibble[4,]$Project <- my_tibble[1,]$Project
I have thought I could use a while loop and a column Weeks_Remaining which decrements each time. So something like this pseudo-code:
While Weeks Remaining > 1, add a row with Week_Start_Day + 7 and decrement Weeks remaining by 1.
I used the below code to duplicate the rows the correct number of times, but as you can see the Week date field did not increment.
my_tibble_1 <- data.frame(lapply(my_tibble, rep, my_tibble$Duration))