4
我想獲得向量中元素之間的序列。這是一個可重現的例子。在一個向量中查找元素之間的序列
vec <- c('a', letters[2:7], 'a', letters[9:14], 'a', letters[16:21]) # sample data
ind_a <- which(grepl('a', vec)) # indices matching the character 'a'
ind_a <- c(ind_a, length(vec)+1) # concatenate the length of 'vec' + 1 with indices
ind_a
# [1] 1 8 15 22
現在,如何計算ind_a
元素之間的序列。例如,seq(from = 2, to = 7, by = 1)
和其他人一樣。
最好,我想知道在這個任務基地R的任何功能。
期望輸出
# List of 3
# $ : int [1:6] 2 3 4 5 6 7
# $ : int [1:6] 9 10 11 12 13 14
# $ : int [1:6] 16 17 18 19 20 21
替代語法:'Map(seq,head(ind_a,-1)+ 1,tail(ind_a,-1)-1)' – Zelazny7