2

我有一个data.frame由每个线性间隔组成的id

df <- data.frame(id = c(rep("a",3),rep("b",4),rep("d",4)),
                 start = c(3,4,10,5,6,9,12,8,12,15,27),
                 end = c(7,8,12,8,9,13,13,10,15,26,30))

我正在寻找一个有效的函数,它将每个id. df结果将是:

res.df <- data.frame(id = c("a","a","b","d","d","d"),
                     start = c(3,10,5,8,12,27),
                     end = c(8,12,13,10,26,30))

为此,我最终将能够总结每个联合间隔id以获得它们的组合长度:

sapply(unique(res.df$id), function(x) sum(res.df$end[which(res.df$id == x)]-res.df$start[which(res.df$id == x)]+1))
4

1 回答 1

3
#source("https://bioconductor.org/biocLite.R")
#biocLite("IRanges")
library(IRanges)
df1 <- as(df, "RangedData")

as.data.frame(reduce(df1, by = "id", min.gapwidth = 0.5))

#  space start end width id
#1     1     3   8     6  a
#2     1    10  12     3  a
#3     1     5  13     9  b
#4     1     8  10     3  d
#5     1    12  26    15  d
#6     1    27  30     4  d
于 2016-03-21T11:40:37.700 回答