本文介绍了如何在R中将间隔数据合并为更少的间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将一系列间隔缩小为更少的,同样有意义的间隔.
I am trying to collapse a series of intervals into fewer, equally meaningful intervals.
例如考虑以下间隔列表
Intervals = list(
c(23,34),
c(45,48),
c(31,35),
c(7,16),
c(5,9),
c(56,57),
c(55,58)
)
由于间隔重叠,因此可以用很少的向量描述相同的间隔.绘制这些间隔可以很明显地看到,只要有四个向量就足够了
Because the intervals overlap, the same intervals can be described with few vectors. Plotting these intervals make obvious that a list of 4 vectors would be enough
plot(1,1,type="n",xlim=range(unlist(Intervals)),ylim=c(0.9,1.1))
segments(
x0=sapply(Intervals,"[",1),
x1=sapply(Intervals,"[",2),
y0=rep(1,length(Intervals)),
y1=rep(1,length(Intervals)),
lwd=10
)
如何减少我的Intervals
列表以携带与情节上显示的信息相同的信息? (性能问题)
How can I reduce my Intervals
list to carry the same info than the one displayed on the plot? (performance matter)
以上示例的期望输出是
Intervals = list(
c(5,16)
c(23,35),
c(45,48),
c(55,58)
)
推荐答案
您需要的是IRanges
包中的reduce
函数.
What you need is the reduce
function in the IRanges
package.
In.df <- do.call(rbind, Intervals)
library(IRanges)
In.ir <- IRanges(In.df[, 1], In.df[,2])
out.ir <- reduce(In.ir)
out.ir
# IRanges of length 4
# start end width
# [1] 5 16 12
# [2] 23 35 13
# [3] 45 48 4
# [4] 55 58 4
这篇关于如何在R中将间隔数据合并为更少的间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!