Generate data frame help using rep or seq

turno.pdf (223.8 KB)
I canot generate column Team

1 Like

I think we need more information.See
FAQ Asking Questions

start_date <- as.Date("2024-03-01")
end_date <- as.Date("2024-03-31")
s = c("a", "b", "c","d");s
date_seq <- rep(seq(from = start_date, to = end_date, by = "day"),each=3)
Turno<-rep(1:3,31)
Team<-rep(s,93)

df <- data.frame(
date = date_seq,Turno,Team )
df

1 Like

It seems fine to me? What is the problem with Team

is your issue is that you intended it to start with d ? but you started it with a ?

start_date <- as.Date("2024-03-01")
end_date <- as.Date("2024-03-31")
s = c("d", "a", "b","c");s
date_seq <- rep(seq(from = start_date, to = end_date, by = "day"),each=3)
Turno<-rep(1:3,31)
Team<-rep(s,93)


df <- data.frame(
    date = date_seq,Turno,Team )
df
######
I want to obtain in column Team:
Team 
d
a
b
c
a
b
c
a
b
c
d
b
c
d
a
c
d
a
b
d
a
b
d
a
b
c
a
b
c
d
b
c
d
a
c
d
a
c
d
a
b
d
a
b
c
a
b
c
d
b
c
d
b
c
d
a
c
d
a
b
d
a
b
c
a
b
c
a
b
c
d
b
c
d
a
c
d
a
b
d
a
b
d
a
b
c
a
b
c
d
b
c
d

wait, is there supposed to be some pattern to team ? it seems abritrary /random.
if its completely specified then you will have to fully specify it; you can only use something like repetition if it repeats.

To follow up on nirgraham's question , if you are setting some kind of pattern it is off by one:

Team
 a  b  c  d 
23 24 23 23 

Can you give us a description in conceptual terms of what that table should be?

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

Thanks a lot .You are the best

Sorry am using Rstudio Version 1.1.456
a I got error ,
Error: unexpected input in "map(diff_pattern_seed, "
after
map(diff_pattern_seed, (c) rep(c, 7)) %>%
as_tibble(c() %>%
slice(1:31) -> full_diff_pattern

The error you posted looks like it shows a typo in the code — I assume you had to edit the code because your RStudio version showed there problems with the code before you even tried to run it, is that right?

I tried to run the code but the error apear after the line (
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

Repeat pattern to cover 31 days

map(diff_pattern_seed, (c) rep(c, 7)) |>
as_tibble() |>
slice(1:31) -> full_diff_pattern
##error here:Error in vec_is(x) : argument "x" is missing, with no default

That code also shows a typo, so I'm guessing you had no choice but to edit the code before running, is that right?

@danut_horincas ; We ask posters here to take care that code is properly formatted. It makes the site easier to read, prevents confusion (unformatted code can mistakenly trigger other types of automatic formatting) and generally is considered the polite thing to do. You can read all the details of how to do it here: FAQ: How to make your code look nice? Markdown Formatting

Or try the easy way :grin: : select any code (whether in blocks or in bits and pieces mixed into your sentences) and click the little </> button at the top of the posting box.

Thanks for pointing to this, Nir — I see now the backslashes in the code disappeared when Danut posted the code outside of a code block.

Danut: I'm sure it's possible to come up with alternative code for your R/RStudio setup, but it's hard to know how to advise you since your RStudio (and also R?) version is very old. Can you not update them?

map(diff_pattern_seed, (c) rep(c, 7)) |>
as_tibble() |>
slice(1:31) -> full_diff_pattern
##error here:Error in vec_is(x) : argument "x" is missing, with no default

There should be a backslash before the (c), so:

map(diff_pattern_seed, \(c) rep(c, 7))

but it may trigger an error if the code is out of sync with your R package versions. Maybe try it with the correction first, and then go from there?

#My last comment ,error below
The x argument of as_tibble() can't be missing as of tibble 3.0.0. Preformatted text

Thanks, Danut: It's a little difficult to tell whether we're on the same page, so could you do the following?

  1. Copy and paste the all of content from the first block from the code I posted (and only the first block).
  2. Select all of that content (and only that content) and run reprex() on it. (If you're not familiar with reprex, see this post.)
  3. Past the reprex() output here, as is.

That should help us see where to go next.