使用R选择一个表中来自另一个表中两列范围的行

使用R选择一个表中来自另一个表中两列范围的行,r,merge,data.table,R,Merge,Data.table,只有当行在“ref”表的间隔内时,我才需要保存“map”中的行: 按照“映射”表的示例进行操作: map首先,您需要扩展间隔: L <- lapply(split(ref,ref$chr), function(d) unique(unlist(mapply(seq,d$start,d$end,SIMPLIFY = F)))) 然后您可以合并: ref2 <- setNames(stack(L),c('start','chr')) merge(map,ref2) 首先,您需要扩展间

只有当行在“ref”表的间隔内时,我才需要保存“map”中的行:

按照“映射”表的示例进行操作:


map首先,您需要扩展间隔:

L <- lapply(split(ref,ref$chr), function(d) unique(unlist(mapply(seq,d$start,d$end,SIMPLIFY = F))))
然后您可以合并:

ref2 <- setNames(stack(L),c('start','chr'))
merge(map,ref2)

首先,您需要扩展间隔:

L <- lapply(split(ref,ref$chr), function(d) unique(unlist(mapply(seq,d$start,d$end,SIMPLIFY = F))))
然后您可以合并:

ref2 <- setNames(stack(L),c('start','chr'))
merge(map,ref2)

由于这是用
data.table
tag标记的,这里有一个简单的
data.table::forverlaps
解决方案

setDT(map)[, end := start]
setkey(setDT(ref))
indx <- unique(foverlaps(map, ref, which = TRUE, nomatch = 0L)$xid)
map[indx]
#     chr start    tag depth BCV State end
# 1: chr1     1 chr1-1     1   2     1   1
# 2: chr1     2 chr1-2     1   3     2   2
# 3: chr1     3 chr1-3     1   2     3   3
# 4: chr2     7 chr2-7     3   2     9   7
# 5: chr2     8 chr2-8     2   2     2   8
# 6: chr2     9 chr2-9     2   2     1   9
setDT(map)[,end:=start]
setkey(setDT(参考))

indx因为这是用
数据标记的。table
标记,这里有一个简单的
数据。table::forverlaps
解决方案

setDT(map)[, end := start]
setkey(setDT(ref))
indx <- unique(foverlaps(map, ref, which = TRUE, nomatch = 0L)$xid)
map[indx]
#     chr start    tag depth BCV State end
# 1: chr1     1 chr1-1     1   2     1   1
# 2: chr1     2 chr1-2     1   3     2   2
# 3: chr1     3 chr1-3     1   2     3   3
# 4: chr2     7 chr2-7     3   2     9   7
# 5: chr2     8 chr2-8     2   2     2   8
# 6: chr2     9 chr2-9     2   2     1   9
setDT(map)[,end:=start]
setkey(setDT(参考))
indx
#   chr start    tag depth BCV State
#1 chr1     1 chr1-1     1   2     1
#2 chr1     2 chr1-2     1   3     2
#3 chr1     3 chr1-3     1   2     3
#4 chr2     7 chr2-7     3   2     9
#5 chr2     8 chr2-8     2   2     2
#6 chr2     9 chr2-9     2   2     1
setDT(map)[, end := start]
setkey(setDT(ref))
indx <- unique(foverlaps(map, ref, which = TRUE, nomatch = 0L)$xid)
map[indx]
#     chr start    tag depth BCV State end
# 1: chr1     1 chr1-1     1   2     1   1
# 2: chr1     2 chr1-2     1   3     2   2
# 3: chr1     3 chr1-3     1   2     3   3
# 4: chr2     7 chr2-7     3   2     9   7
# 5: chr2     8 chr2-8     2   2     2   8
# 6: chr2     9 chr2-9     2   2     1   9