data frame lookup value in range and return different column

前端 未结 2 680
心在旅途
心在旅途 2021-01-06 05:14

I have two data frames and wish to use the value in one (DF1$pos) to search through two columns in DF2 (DF2start, DF2end) and if it falls within those numbers,

2条回答
  •  走了就别回头了
    2021-01-06 06:01

    You could also use IRanges

    source("http://bioconductor.org/biocLite.R")
    biocLite("IRanges")
    library(IRanges)
    DF1N <- with(DF1, IRanges(pos, pos))
    DF2N <- with(DF2, IRanges(start, end))
    DF1$name <- DF2$annot[subjectHits(findOverlaps(DF1N, DF2N))]
    DF1
    #   ID pos name
    #1 chr  12   a1
    #2 chr 542   a3
    #3 chr 674   a3
    

提交回复
热议问题