I want to generate a sequence where in a first step a random start number is chosen. Depending on the random start number (let's say: start_number = 3
) I want to generate the following sequence. How can I do this?
seq_length <- 4
start_number <- sample(1:seq_length, 1, replace = TRUE)
# Desired sequence
int [1:100] 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 ...
You can use the rep
etition function:
rep(c(3,4,1,2), each = 2, length = 100)
num [1:100] 3 3 4 4 1 1 2 2 3 3 ...
if you want integers:
rep(c(3L,4L,1L,2L), each = 2, length = 100)
int [1:100] 3 3 4 4 1 1 2 2 3 3 ...
Edit:
Since you said your start is random, here is a code to do this:
seq_length <- 4
start_number <- sample(seq_length, 1)
b <- start_number:seq_length
rep(c(b, setdiff(1:seq_length, b)), each = 2, length=100)
[1] 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 ....