使用两个向量构建索引向量,其中第一个向量包含起始索引,第二个向量使用R

使用两个向量构建索引向量,其中第一个向量包含起始索引,第二个向量使用R,r,R,我有两个向量: startIdxes<-c(1, 5, 12, 22) endIdxes<-c(2, 9, 17, 25) 我们可以使用Map来获得向量s和未列出列表输出中相应值之间的顺序 unlist(Map(`:`, startIdxes, endIdxes)) #[1] 1 2 5 6 7 8 9 12 13 14 15 16 17 22 23 24 25 功能Map为 Map function (f, ...) { f <- match.fun(

我有两个向量:

startIdxes<-c(1, 5, 12, 22)
endIdxes<-c(2, 9, 17, 25)

我们可以使用
Map
来获得
向量
s和
未列出
列表输出中相应值之间的顺序

unlist(Map(`:`, startIdxes, endIdxes))
#[1]  1  2  5  6  7  8  9 12 13 14 15 16 17 22 23 24 25
功能
Map

Map
function (f, ...) 
{
  f <- match.fun(f)
    mapply(FUN = f, ..., SIMPLIFY = FALSE)
}

您可以使用
mapply

unlist(mapply(seq,startIdxes,endIdxes)) 

#[1]  1  2  5  6  7  8  9 12 13 14 15 16 17 22 23 24 25
i1 <- endIdxes - startIdxes
sort(c(startIdxes, rep(startIdxes, i1) +  sequence(i1)))
#[1]  1  2  5  6  7  8  9 12 13 14 15 16 17 22 23 24 25
unlist(mapply(seq,startIdxes,endIdxes)) 

#[1]  1  2  5  6  7  8  9 12 13 14 15 16 17 22 23 24 25