Repeat pattern in R (integers up and down)

I would like to create a repeat pattern from 5 to 0, then go back to 5, 3 times, so I want 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, ..., 5 How I can do it? R?

I know rep

(seq(from=a, by=b, length=c),d)

but I don’t know how to use it with this template ... Can someone help me, I would like to use

x <- 5
y <- 3
+4
source share
2 answers

We can use seqwith rev. Since we do not want repetitions of 5 and 0 twice, I did not include them in the command seq. We repeat seq(4, 1)its inverse ( rev) three times.

a = seq(4, 1)
c(rep(c(5, a, 0, rev(a)), 3), 5)

#[1] 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5
+3
source

. 5:0 0:5. . , rle , , , 1, 1, inverse.rle .

x <- rep(c(5:0, 0:5), 3)
y <- rle(x)
y$lengths[y$lengths > 1] <- 1
z <- inverse.rle(y)
z
[1] 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5
0

Source: https://habr.com/ru/post/1686516/


All Articles