Search code examples
rvectorintervals

Combine 2 Vectors


I am quite a beginner in R and I try to simplify my current code.

So I want to define intervals between 1 and 'nb' by 'step' increment

Let's say that nb = 3200 and step = 1000

The result should be

[[1]]
[1]    1 1001

[[2]]
[1] 1001 2001

[[3]]
[1] 2001 3001

[[4]]
[1] 3001 3200

I have those two vectors

X: [1]     1 1001 2001 3001
M: [1]  1000 2000 3000 3200

Edit: I finaly found a way to do that

X <- seq(1, nb, step)
M <- pmin(X + step - 1, nb)
tmp <- cbind(X, M)
intervals <- split(tmp, row(tmp))

Maybe someone could have a faster solution ?


Solution

  • X <- seq(1, nb, step)
    M <- pmin(X + step - 1, nb)
    tmp <- cbind(X, M)
    intervals <- split(tmp, row(tmp))