0

I am searching for a simple and elegant (basic R functions, no for cyclus) mechanism as function(start:stop) which would create, for example function(2:5) the following vector sequence:

c(2:5, 3:5, 4:5, 5:5)

2,3,4,5,3,4,5,4,5,5

I have tried to put this into the function seq(). Sadly, function seq() does not allow for vector in the argument: from=....

Do you know some solution?

Thank you very much

Henrik
  • 65,555
  • 14
  • 143
  • 159
Athaeneus
  • 131
  • 6

1 Answers1

1

We may use rep

x1 <- 2:5 + rep(0:3, each = 4)
x1[x1 <6]
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Just for clarity, do you have an idea which of these options is more efficient (computationally) if made for general use "function(start,stop)"? In "unlist" case we work with lists and in "rep" case we omit elements from vector. – Athaeneus Jan 17 '23 at 19:05
  • 1
    @Athaeneus If there are lots of elements, probably the `rep` would be faster compared to loop with `sapply` – akrun Jan 17 '23 at 19:06