2016-08-03 47 views

回答

2

我们可以使用Map获得相应的值之间既vector S和unlistlist输出序列。

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) 
} 

另一种选择是要使两个向量之间的差,以“startIdxes”与差的sequence复制添加,与原来的串联'startIdxes'和sort

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 
1

您可以使用mapply

unlist(mapply(seq,startIdxes,endIdxes)) 

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