Home > Software design >  find a list of interval values from a vector in r
find a list of interval values from a vector in r

Time:01-19

Say I have a numeric vector as in X = 1:10000. Is there currently a way in R to obtain a list of 10 intervals from X as shown in my Desired_output?

Desired_output = list(c(0,1000), c(1001,2000),  c(2001,3000),  c(3001,4000),  c(4001,5000),  
                      c(5001,6000), c(6001,7000), c(7001,8000), c(8001,9000), c(9001,10000))

CodePudding user response:

Use seq

Map(`c`, c(0, seq(1001, 10000, by = 1000)), seq(1000, 10000, by = 1000))

-output

[[1]]
[1]    0 1000

[[2]]
[1] 1001 2000

[[3]]
[1] 2001 3000

[[4]]
[1] 3001 4000

[[5]]
[1] 4001 5000

[[6]]
[1] 5001 6000

[[7]]
[1] 6001 7000

[[8]]
[1] 7001 8000

[[9]]
[1] 8001 9000

[[10]]
[1]  9001 10000
  • Related