Home > OS >  How can I write a loop that adds a specified number of rows to this data frame?
How can I write a loop that adds a specified number of rows to this data frame?

Time:06-02

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:

  1. Each project has a start day (Week_Start_Day) — the day work begins
  2. Each project has a Duration in weeks
  3. Each project demands x hours per Week (Weekly_Hours)
  4. 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))  

CodePudding user response:

This might get you close to what you are looking for:

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")
) %>% 
  mutate(
    weeks = map2(Week_Start_Day, Duration, ~ .x   weeks(1:.y-1))
  ) %>% 
  unnest(c(weeks))

This will generate a new column weeks containing the start date for each of the weeks over the defined duration.

CodePudding user response:

No need for loops here:

 my_tibble %>% 
   group_by_all() %>% 
   summarize(Week_beginning = Week_Start_Day   7 * (seq(Duration) - 1)) 
#>    Week_Start_Day Duration Weekly_Hours Project Week_beginning
#>    <date>            <dbl>        <dbl> <chr>   <date>        
#>  1 2022-06-06            8         10   ClientA 2022-06-06    
#>  2 2022-06-06            8         10   ClientA 2022-06-13    
#>  3 2022-06-06            8         10   ClientA 2022-06-20    
#>  4 2022-06-06            8         10   ClientA 2022-06-27    
#>  5 2022-06-06            8         10   ClientA 2022-07-04    
#>  6 2022-06-06            8         10   ClientA 2022-07-11    
#>  7 2022-06-06            8         10   ClientA 2022-07-18    
#>  8 2022-06-06            8         10   ClientA 2022-07-25    
#>  9 2022-06-20            1          5.5 ClientB 2022-06-20    
#> 10 2022-07-04            3         15.7 ClientC 2022-07-04    
#> 11 2022-07-04            3         15.7 ClientC 2022-07-11    
#> 12 2022-07-04            3         15.7 ClientC 2022-07-18  
  • Related