Generate data frame help using rep or seq

Does this work for you?

library(tidyverse)

# Record pattern of day-to-day differences
tribble(
  ~turno1, ~turno2, ~turno3,
  0, 0, 0,
  -1, 0, 0, 
  0, 0, 0, 
  0, -1, 0,
  0, 0, -1
) -> diff_pattern_seed

# Repeat pattern to cover 31 days 
map(diff_pattern_seed, \(c) rep(c, 7)) |>
  as_tibble() |> 
  slice(1:31) -> full_diff_pattern
# Apply difference pattern to obtain teams
full_diff_pattern |> 
  # calculate cumulative effect of day-to-day differences
  mutate(across(everything(), cumsum)) |> 
  # apply differences starting from d a b interpreted as integers mod 4
  map2(c(3, 0, 1), \(c, n) (c + n) %% 4) |> 
  as_tibble() |> 
  # convert integers mod 4 back to letters
  mutate(across(everything(), \(n) letters[n + 1])) -> teams
# Add date column
tibble(date = seq(ymd('2024-03-01'), by = 'days', length.out = 31)) |> 
  bind_cols(teams) 
#> # A tibble: 31 × 4
#>    date       turno1 turno2 turno3
#>    <date>     <chr>  <chr>  <chr> 
#>  1 2024-03-01 d      a      b     
#>  2 2024-03-02 c      a      b     
#>  3 2024-03-03 c      a      b     
#>  4 2024-03-04 c      d      b     
#>  5 2024-03-05 c      d      a     
#>  6 2024-03-06 c      d      a     
#>  7 2024-03-07 b      d      a     
#>  8 2024-03-08 b      d      a     
#>  9 2024-03-09 b      c      a     
#> 10 2024-03-10 b      c      d     
#> # ℹ 21 more rows
# Reshape table to conform to original in given pdf
tibble(date = seq(ymd('2024-03-01'), by = 'days', length.out = 31)) |> 
  bind_cols(teams) |> 
  pivot_longer(!date, names_to = "turno", values_to = "team")
#> # A tibble: 93 × 3
#>    date       turno  team 
#>    <date>     <chr>  <chr>
#>  1 2024-03-01 turno1 d    
#>  2 2024-03-01 turno2 a    
#>  3 2024-03-01 turno3 b    
#>  4 2024-03-02 turno1 c    
#>  5 2024-03-02 turno2 a    
#>  6 2024-03-02 turno3 b    
#>  7 2024-03-03 turno1 c    
#>  8 2024-03-03 turno2 a    
#>  9 2024-03-03 turno3 b    
#> 10 2024-03-04 turno1 c    
#> # ℹ 83 more rows

Created on 2024-03-25 with reprex v2.0.2

Full reprex in single code block
library(tidyverse)

# Record pattern of day-to-day differences
tribble(
  ~turno1, ~turno2, ~turno3,
  0, 0, 0,
  -1, 0, 0, 
  0, 0, 0, 
  0, -1, 0,
  0, 0, -1
) -> diff_pattern_seed

# Repeat pattern to cover 31 days 
map(diff_pattern_seed, \(c) rep(c, 7)) |>
  as_tibble() |> 
  slice(1:31) -> full_diff_pattern

# Apply difference pattern to obtain teams
full_diff_pattern |> 
  # calculate cumulative effect of day-to-day differences
  mutate(across(everything(), cumsum)) |> 
  # apply differences starting from d a b interpreted as integers mod 4
  map2(c(3, 0, 1), \(c, n) (c + n) %% 4) |> 
  as_tibble() |> 
  # convert integers mod 4 back to letters
  mutate(across(everything(), \(n) letters[n + 1])) -> teams

# Add date column
tibble(date = seq(ymd('2024-03-01'), by = 'days', length.out = 31)) |> 
  bind_cols(teams) 
#> # A tibble: 31 × 4
#>    date       turno1 turno2 turno3
#>    <date>     <chr>  <chr>  <chr> 
#>  1 2024-03-01 d      a      b     
#>  2 2024-03-02 c      a      b     
#>  3 2024-03-03 c      a      b     
#>  4 2024-03-04 c      d      b     
#>  5 2024-03-05 c      d      a     
#>  6 2024-03-06 c      d      a     
#>  7 2024-03-07 b      d      a     
#>  8 2024-03-08 b      d      a     
#>  9 2024-03-09 b      c      a     
#> 10 2024-03-10 b      c      d     
#> # ℹ 21 more rows

# Reshape table to conform to original in given pdf
tibble(date = seq(ymd('2024-03-01'), by = 'days', length.out = 31)) |> 
  bind_cols(teams) |> 
  pivot_longer(!date, names_to = "turno", values_to = "team")
#> # A tibble: 93 × 3
#>    date       turno  team 
#>    <date>     <chr>  <chr>
#>  1 2024-03-01 turno1 d    
#>  2 2024-03-01 turno2 a    
#>  3 2024-03-01 turno3 b    
#>  4 2024-03-02 turno1 c    
#>  5 2024-03-02 turno2 a    
#>  6 2024-03-02 turno3 b    
#>  7 2024-03-03 turno1 c    
#>  8 2024-03-03 turno2 a    
#>  9 2024-03-03 turno3 b    
#> 10 2024-03-04 turno1 c    
#> # ℹ 83 more rows

Created on 2024-03-27 with reprex v2.0.2

2 Likes