匹配序列。获取遵循该模式的元素的索引

时间:2018-07-20 08:25:52

标签: r sequence matching

set.seed(3)

pattern <- letters[1:4]
#[1] "a" "b" "c" "d"
vec<-sample(letters[1:4],25,replace=T)
names(vec) <- seq_along(vec)

数据:

# 1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26 
#"a" "d" "b" "b" "c" "c" "a" "b" "c" "c" "c" "c" "c" "c" "d" "d" "a" "c" "d" "b" "a" "a" "a" "a" "a" "d" 

所需结果:

c(1,3,5,15,17,20) #<< This is the desired result I want to find

# 1   3   5  15  17  20   #<< (This is for explanation purpose)
#"a" "b" "c" "d" "a" "b"

2 个答案:

答案 0 :(得分:2)

最近已经解决了类似的问题here

pattern <- letters[1:4]
keep <- logical(length(vec))

s <- 0
for (i in seq_along(keep)){
    if (pattern[s +1] == vec[i]){
        keep[i] <- T
        s <- (s + 1) %% length(pattern)
    } else {
        keep[i] <- F
    }
}


vec_filtered <- vec[keep]

修改完问题后,您想要的是什么

which(keep)

答案 1 :(得分:1)

使用传统编程方法的另一种方式

#Initialise index to loop through "pattern" and "vec"
i = 1
j = 1
#Create a new variable to store index values
index = numeric()
#Until "vec" is not over
while (i < length(vec)) {
   #Check if a match is found
   if(vec[i] == pattern[j]) {
     #If found store the index
     index = c(index, i)
     j = j + 1
     #Recycle the "pattern" object and continue searching till "vec" is not over
     if (j > length(pattern))
       j = 1
    }
   i = i + 1
}

index
#[1]  1  3  5 15 17 20