This function might be helpful: bleh <- function(a, b) { where <- list() matches <- 0 first <- which(a == b[1]) for (i in first) { seq.to.match <- seq(i, length = length(b)) if (identical(a[seq.to.match], b)) { matches <- matches + 1 where[[matches]] <- seq.to.match } } return(where) }
a<-c(3,4,3,0,4,5,6,9,3,4) b<-c(0,4,5) c<-c(5,4,0) d<-c(3,4) bleh(a, b) bleh(a, c) bleh(a, d) Cheers, Gustavo. On Tue, Sep 21, 2010 at 11:31 AM, Lorenzo Isella <lorenzo.ise...@gmail.com> wrote: > Dear All, > Consider a simple example > > a<-c(1,4,3,0,4,5,6,9,3,4) > b<-c(0,4,5) > c<-c(5,4,0) > > I would like to be able to tell whether a sequence is contained (the order > of the elements does matter) in another one e.g. in the example above, b is > a subsequence of a, whereas c is not. Since the order matters, I cannot > treat the sequences above as sets (also, elements are repeated). > Does anyone know a smart way of achieving that? > Many thanks > > Lorenzo > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.