R: matrix to indexes

后端 未结 3 1798
闹比i
闹比i 2021-01-24 05:31

I have a matrix like

      [,1] [,2]
 [1,]    1    3
 [2,]    4    6
 [3,]   11   12
 [4,]   13   14

I want to convert this matrix to a vector

3条回答
  •  无人及你
    2021-01-24 06:12

    Here's an answer using IRanges package:

    require(IRanges)
    xx.ir <- IRanges(start = xx[,1], end = xx[,2])
    as.vector(coverage(xx.ir))
    # [1] 1 1 1 1 1 1 0 0 0 0 1 1 1 1
    

    If you specify a min and max value of your entire vector length, then:

    max.val <- 20
    min.val <- 1
    c(rep(0, min.val-1), as.vector(coverage(xx.ir)), rep(0, max.val-max(xx)))
    

提交回复
热议问题