Finding overlap in ranges with R

后端 未结 6 915
面向向阳花
面向向阳花 2020-12-01 05:13

I have two data.frames each with three columns: chrom, start & stop, let\'s call them rangesA and rangesB. For each row of rangesA, I\'m looking to find which (if any)

6条回答
  •  囚心锁ツ
    2020-12-01 05:16

    For your example data:

    rangesA <- data.frame(
        chrom = c(5, 1, 9),
        start = c(100, 200, 275),
        stop = c(105, 250, 300)
    )
    rangesB <- data.frame(
        chrom = c(1, 5, 9),
        start = c(200, 99, 275),
        stop = c(265, 106, 290)
    )
    

    This will do it with sapply, such that each column is one row in rangesA and each row is corresponding row in rangesB:

    > sapply(rangesA$stop, '>=', rangesB$start) & sapply(rangesA$start, '<=', rangesB$stop)
          [,1]  [,2]  [,3]
    [1,] FALSE  TRUE FALSE
    [2,]  TRUE FALSE FALSE
    [3,] FALSE FALSE  TRUE
    

提交回复
热议问题