Matching a sequence in a larger vector

前端 未结 2 1059
逝去的感伤
逝去的感伤 2020-11-30 13:58

I\'d like to have a function which returns the initial indicies of matching subsequences of a vector. For example:

y <- c(\"a\",\"a\",\"a\",\"b\",\"c\")

         


        
2条回答
  •  清歌不尽
    2020-11-30 14:37

    Try rollapply in zoo:

    library(zoo)
    which(rollapply(y, 2, identical, c("a", "a")))
    ## [1] 1 2
    which(rollapply(y, 2, identical, c("a", "b")))
    ## [1] 3
    

提交回复
热议问题