2017-08-04 42 views
0

我有長度的矢量創建從號碼矢量索引,中的R

lens <- c(3,4,6,8) 

我想創建從該載體中,其表示在每個索引指數的總數指數。預期產出將爲1:3,4:7,8:13,14:21。如果在一個表的形式,這將是

start_idx end_idx 
1   3 #length of index is from length vector 
4   7 
8   13 
14   21 
+0

接受了gcons – Jian

回答

2

看來你需要的cumsum這裏:

end_idx <- cumsum(lens) 
start_idx <- c(0, head(end_idx,-1)) + 1 

data.frame(start_idx, end_idx) 

# start_idx end_idx 
#1   1  3 
#2   4  7 
#3   8  13 
#4  14  21 
+3

尼斯的答案,但它也可以在燒毛線實現答案:' data.frame(start_idx = cumsum(鏡頭)-lens + 1,end_idx = cumsum(鏡頭))' – gcons

+0

@gcons不錯的一個眼線。 – Psidom

+0

謝謝gcons和psidom – Jian