I have a vector containing user_id (1,2,3,4,5), and i want to create a dataset where each user_id is repeated 3 times:
userid time
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
...
On
df <- data.frame(
user_id = rep(1:5, each = 3),
time = rep(1:3, times = 5)
)
df
#> user_id time
#> 1 1 1
#> 2 1 2
#> 3 1 3
#> 4 2 1
#> 5 2 2
#> 6 2 3
#> 7 3 1
#> 8 3 2
#> 9 3 3
#> 10 4 1
#> 11 4 2
#> 12 4 3
#> 13 5 1
#> 14 5 2
#> 15 5 3
Created on 2022-07-07 by the reprex package (v2.0.1)
You can use
repwith argumenteach = 3or
expand.grid