1

I have the following vector

x <- c(1,3,4,5,8,9,10,13)

I wish to transform it into a set of intervals:

iwant <-  list(1, c(3,5), c(8,10), 13)
iwant
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 3 5
#> 
#> [[3]]
#> [1]  8 10
#> 
#> [[4]]
#> [1] 13

is there an easy way to tranform x such into groups of interval ranges in R or with Rcpp (real x vector may be have up to ~ 80M values) that I've completely missed? If not can someone point me to an algorithm I can't seem to figure it out.

pat shipan
  • 715
  • 6
  • 13
  • Interesting question. I think you can modify the answer here: http://stackoverflow.com/q/16800803/1191259 or at least use it as a starting point to code your own Rcpp solution. – Frank Sep 17 '15 at 22:24
  • How are those intervals defined? Quantiles? Maximum difference between elements? Other (what)? Why not (1,4),(5,9),(10,13)? Not totally sure I'm understanding what you're asking, but you might want to check `cut` and `Hmisc::cut2` too. – PavoDive Sep 17 '15 at 22:56
  • @PavoDive Just break it up into continuous sequences of integers and it is 1, 3:5, 8:10, 13. – Frank Sep 18 '15 at 01:10

2 Answers2

2

It is not clear waht you are looking to do ,but here how you can reproduce the expected result:

lapply(split(x,c(0,cumsum(diff(x)!=1))),
       function(y)if(length(y)>2) c(y[1],tail(y,1))else y)


$`0`
[1] 1

$`1`
[1] 3 5

$`2`
[1]  8 10

$`3`
[1] 13
agstudy
  • 119,832
  • 17
  • 199
  • 261
1

I'm not exactly sure how you're defining an instance of an interval but here's one way using the IRanges package (installation instructions). It has really good documentation.

require(IRanges)
reduce(IRanges(x, x))
# IRanges of length 4
#     start end width
# [1]     1   1     1
# [2]     3   5     3
# [3]     8  10     3
# [4]    13  13     1
Arun
  • 116,683
  • 26
  • 284
  • 387
Chris
  • 1,575
  • 13
  • 20