I want to create a vector of length n
which will contain sequences of arbitrary length
I made a code that does this, but it seems unreliable and a little ridiculous to me.
n <- 300
vals <- c(-1,0,1)
seq_range <- 3:20
make_seq <- function() sample(vals,size=1) |> rep(sample(seq_range,1))
my_seq <- lapply(1:50,\(x) make_seq()) |> unlist() |> tail(n = n)
...
my_seq
[1] 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[35] 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[69] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 -1 -1 -1 -1 -1 -1 -1
[103] -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[137] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0
[171] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 -1
[205] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0
[239] 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[273] 1 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I create a list and then cut it to size n
, I don't like it...
Is there a way or a ready-made function that can replace my code and make it better?
==============================================================
sequences inside the vector my_seq
have arbitrary length
vector my_seq
has a length n
I want to set the length range of sequences in a vector for example, as in my code rep(sample(3:20,1))
What does it mean that the length of any sequence is from 3
to 20
I want to indicate the elements of sequences, it can be either -1,0,1 or something else with a different set and number of elements, for example 0,1 or 1,5,10,20.
But these will always be int
values
What are you intending to do?
n <- 300
vals <- c(-1, 0, 1)
seq_range <- 3:20
seqs <- sample(seq_range, n %/% min(seq_range), replace = TRUE)
M <- min(which(cumsum(seqs) >= n))
v <- sample(vals, M, replace = TRUE)
rep(v, seqs[1:M])[1:n]