Generate data frame help using rep or seq

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

reprex()

Error in parse(text = x, keep.source = TRUE) :
:18:24: unexpected input
17: # Repeat pattern to cover 31 days
18: map(diff_pattern_seed,
^

Hi Danut,

Please see the link I provided for how to use the reprex() function — otherwise it'll be harder to tell what's going on.

library(tidyverse)
library(reprex)

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

reprex

library(tidyverse)
library(reprex)

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

reprex()

library(tidyverse)
library(reprex)

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

reprex()
i Rendering reprex...
Error in parse(text = x, keep.source = TRUE) :
:18:24: unexpected input
17: # Repeat pattern to cover 31 days
18: map(diff_pattern_seed,
^

Thanks for trying, Danut. I'll try to lay out the necessary steps for you here:

  1. Copy the content of the first code block from the code I posted, and paste in RStudio:
  2. Select all the code:
  3. Click the 'Addins' button:
  4. Select Addins > Reprex selection:
  5. Step 4 will place the output invisibly in your clipboard, so — without doing anything else first — reply here and immediately type CTRL-V to paste the output.

Let us know if you have any questions.

#My solution based Rstudio version:below(but turno are not goud one -I dont know how to eliminate turno form turno1 to obtain only 1 and column team have wrong results:
library(dplyr)
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




  
n <- 7
tura<-do.call("rbind", replicate(n, diff_pattern_seed, simplify = FALSE))
tura
tura <- slice(tura, 1:(n() - 4))     # Apply slice & n functions
tura 

tura1<-tura%>%
  mutate(turno1 = cumsum(turno1),
         turno2 = cumsum(turno2),turno3 = cumsum(turno3))
tura1
tura2<-tura1%>%
  mutate(turno1 = (turno1%%4),
         turno2 = (turno2%%4),turno3=(turno3%%4))
tura2
tura3<-tura2%>%
  mutate(turno1=LETTERS[tura2$turno1+1],turno2=LETTERS[tura2$turno2+1],turno3=LETTERS[tura2$turno3+1])
tura3

tibble(date = seq(ymd('2024-03-01'), by = 'days', length.out = 31)) %>%
  bind_cols(tura3) 
 
x<-tibble(date = seq(ymd('2024-03-01'), by = 'days', length.out = 31)) %>%
  bind_cols(tura3) %>%
  pivot_longer(!date, names_to = "turno", values_to = "team")
x

Looks like you translated the approach to fit your versions of R and RStudio, but it's hard to read your observations — could you edit your post so that your observations (the first line in your code block) are not in the code block?

library(dplyr)
library(tidyverse)

tribble(
~turno1, ~turno2, ~turno3,
0, 0, 0,
-1, 0, 0,
0, 0, 0,
0, -1, 0,
0, 0, -1
) -> diff_pattern_seed

n <- 7
tura<-do.call("rbind", replicate(n, diff_pattern_seed, simplify = FALSE))
tura
tura <- slice(tura, 1:(n() - 4)) # Apply slice & n functions
tura

tura1<-tura%>%
mutate(turno1 = cumsum(turno1),
turno2 = cumsum(turno2),turno3 = cumsum(turno3))
tura1
tura2<-tura1%>%
mutate(turno1 = (turno1%%4),
turno2 = (turno2%%4),turno3=(turno3%%4))
tura2
tura3<-tura2%>%
mutate(turno1=LETTERS[tura2$turno1+1],turno2=LETTERS[tura2$turno2+1],turno3=LETTERS[tura2$turno3+1])
tura3

tibble(date = seq(ymd('2024-03-01'), by = 'days', length.out = 31)) %>%
bind_cols(tura3)

x<-tibble(date = seq(ymd('2024-03-01'), by = 'days', length.out = 31)) %>%
bind_cols(tura3) %>%
pivot_longer(!date, names_to = "turno", values_to = "team")
x

You should keep the code inside the code block (you took it out) — it's your observations and questions in English that should be outside of the code block.

library(dplyr)
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




  
n <- 7
tura<-do.call("rbind", replicate(n, diff_pattern_seed, simplify = FALSE))
tura
tura <- slice(tura, 1:(n() - 4))     # Apply slice & n functions
tura 

tura1<-tura%>%
  mutate(turno1 = cumsum(turno1),
         turno2 = cumsum(turno2),turno3 = cumsum(turno3))
tura1
tura2<-tura1%>%
  mutate(turno1 = (turno1%%4),
         turno2 = (turno2%%4),turno3=(turno3%%4))
tura2
tura3<-tura2%>%
  mutate(turno1=LETTERS[tura2$turno1+1],turno2=LETTERS[tura2$turno2+1],turno3=LETTERS[tura2$turno3+1])
tura3

tibble(date = seq(ymd('2024-03-01'), by = 'days', length.out = 31)) %>%
  bind_cols(tura3) 
 
x<-tibble(date = seq(ymd('2024-03-01'), by = 'days', length.out = 31)) %>%
  bind_cols(tura3) %>%
  pivot_longer(!date, names_to = "turno", values_to = "team")
x


Thanks, Danut, that's the code, but what are your questions or comments at this point? Those aren't clear from the code on its own.

turno.pdf (223.8 KB)
The results are not corect ;See the 2 and 3 columns

Check columns :Turno ,Team

Here is the original table, recreated by using Addins > DATAPASTA > Paste as tribble (requires installation of datapasta package):

Output of pasting as tribble, saved as 'original' (click to open)
original <- 
  tibble::tribble(
    ~Data.Turno.Team,
    "01-03-2024 1 d",
    "01-03-2024 2 a",
    "01-03-2024 3 b",
    "02-03-2024 1 c",
    "02-03-2024 2 a",
    "02-03-2024 3 b",
    "03-03-2024 1 c",
    "03-03-2024 2 a",
    "03-03-2024 3 b",
    "04-03-2024 1 c",
    "04-03-2024 2 d",
    "04-03-2024 3 b",
    "05-03-2024 1 c",
    "05-03-2024 2 d",
    "05-03-2024 3 a",
    "06-03-2024 1 c",
    "06-03-2024 2 d",
    "06-03-2024 3 a",
    "07-03-2024 1 b",
    "07-03-2024 2 d",
    "07-03-2024 3 a",
    "08-03-2024 1 b",
    "08-03-2024 2 d",
    "08-03-2024 3 a",
    "09-03-2024 1 b",
    "09-03-2024 2 c",
    "09-03-2024 3 a",
    "10-03-2024 1 b",
    "10-03-2024 2 c",
    "10-03-2024 3 d",
    "11-03-2024 1 b",
    "11-03-2024 2 c",
    "11-03-2024 3 d",
    "12-03-2024 1 a",
    "12-03-2024 2 c",
    "12-03-2024 3 d",
    "13-03-2024 1 a",
    "13-03-2024 2 c",
    "13-03-2024 3 d",
    "14-03-2024 1 a",
    "14-03-2024 2 b",
    "14-03-2024 3 d",
    "15-03-2024 1 a",
    "15-03-2024 2 b",
    "15-03-2024 3 c",
    "16-03-2024 1 a",
    "16-03-2024 2 b",
    "16-03-2024 3 c",
    "17-03-2024 1 d",
    "17-03-2024 2 b",
    "17-03-2024 3 c",
    "18-03-2024 1 d",
    "18-03-2024 2 b",
    "18-03-2024 3 c",
    "19-03-2024 1 d",
    "19-03-2024 2 a",
    "19-03-2024 3 c",
    "20-03-2024 1 d",
    "20-03-2024 2 a",
    "20-03-2024 3 b",
    "21-03-2024 1 d",
    "21-03-2024 2 a",
    "21-03-2024 3 b",
    "22-03-2024 1 c",
    "22-03-2024 2 a",
    "22-03-2024 3 b",
    "23-03-2024 1 c",
    "23-03-2024 2 a",
    "23-03-2024 3 b",
    "24-03-2024 1 c",
    "24-03-2024 2 d",
    "24-03-2024 3 b",
    "25-03-2024 1 c",
    "25-03-2024 2 d",
    "25-03-2024 3 a",
    "26-03-2024 1 c",
    "26-03-2024 2 d",
    "26-03-2024 3 a",
    "27-03-2024 1 b",
    "27-03-2024 2 d",
    "27-03-2024 3 a",
    "28-03-2024 1 b",
    "28-03-2024 2 d",
    "28-03-2024 3 a",
    "29-03-2024 1 b",
    "29-03-2024 2 c",
    "29-03-2024 3 a",
    "30-03-2024 1 b",
    "30-03-2024 2 c",
    "30-03-2024 3 d",
    "31-03-2024 1 b",
    "31-03-2024 2 c",
    "31-03-2024 3 d"
  )
Cleaning 'original' to match table in pdf file (click to open)
library(tidyverse)

original
#> # A tibble: 93 × 1
#>    Data.Turno.Team
#>    <chr>          
#>  1 01-03-2024 1 d 
#>  2 01-03-2024 2 a 
#>  3 01-03-2024 3 b 
#>  4 02-03-2024 1 c 
#>  5 02-03-2024 2 a 
#>  6 02-03-2024 3 b 
#>  7 03-03-2024 1 c 
#>  8 03-03-2024 2 a 
#>  9 03-03-2024 3 b 
#> 10 04-03-2024 1 c 
#> # ℹ 83 more rows
# Separate single column from datapasta output into three columns and
# intepret date strings as data objects
original |> 
  separate_wider_delim(
    cols = Data.Turno.Team, 
    names = c('date', 'turno', 'team'), 
    delim = " "
  ) |> 
  mutate(date = parse_date(date, format = '%d-%m-%Y')) -> original

original
#> # A tibble: 93 × 3
#>    date       turno team 
#>    <date>     <chr> <chr>
#>  1 2024-03-01 1     d    
#>  2 2024-03-01 2     a    
#>  3 2024-03-01 3     b    
#>  4 2024-03-02 1     c    
#>  5 2024-03-02 2     a    
#>  6 2024-03-02 3     b    
#>  7 2024-03-03 1     c    
#>  8 2024-03-03 2     a    
#>  9 2024-03-03 3     b    
#> 10 2024-03-04 1     c    
#> # ℹ 83 more rows

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

You can now use the table original to compare directly to the output of any attempts to recreate it.

How to compare table original to an attempt at recreating it

Suggested solution from above:

Full solution reprex, final table saved as 'reproduction' (click to open)
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") -> reproduction

reproduction
#> # 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
Cleaning 'reproduction' to remove string 'turno' from 2nd column (click to open)
reproduction
#> # 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

# Remove string "turno" from column 'turno'
reproduction |> 
    mutate(turno = turno |> str_remove('turno')) -> reproduction

reproduction
#> # A tibble: 93 × 3
#>    date       turno team 
#>    <date>     <chr> <chr>
#>  1 2024-03-01 1     d    
#>  2 2024-03-01 2     a    
#>  3 2024-03-01 3     b    
#>  4 2024-03-02 1     c    
#>  5 2024-03-02 2     a    
#>  6 2024-03-02 3     b    
#>  7 2024-03-03 1     c    
#>  8 2024-03-03 2     a    
#>  9 2024-03-03 3     b    
#> 10 2024-03-04 1     c    
#> # ℹ 83 more rows
# Compare tables 'original' and 'reproduction'
all(original == reproduction)
#> [1] TRUE

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

I want result of data frane x to look like turno.pdf.Of cirse the content should becidentical

So to confirm what you're asking now: You would like help modifying the code you developed to construct the table x, so that x matches the original pdf table — correct?

Since many people read this forum, including folks who may not know how to compare tables yet, I included information both on how to clean data extracted from the pdf and on how to compare tables.

Ye s confirm .I am not able to obtain the same results like in Turno.pdf the same column name and the same results .I am blocked seems :map2(c(3, 0, 1), (c, n) (c + n) %% 4) |>
as_tibble() |>``

I think this should work in place of the map2() code:

tura2 <-
  tura1 %>%
  mutate(
    turno1 = (turno1 + 3) %% 4,
    turno2 = (turno2 + 0) %% 4,
    turno3 = (turno3 + 1) %% 4
  )
1 Like