[R] searching several subsequences in a single string sequence

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Sep 27 23:25:43 CEST 2011


On Tue, Sep 27, 2011 at 6:15 PM, Jean V Adams <jvadams at usgs.gov> wrote:

> For example,
>
> songs <- c("ABCABAABABABCAB", "ABACAB", "ABABCABCBC")
> counts <- gregexpr("ABC", songs)
> sapply(counts, length)

 That will still return '1' for the case where its not found, because of the -1.

sapply(counts,function(x){sum(x>0)})

 will return 0.

 you might get faster performance if you do 'fixed=TRUE' on the
gregexpr call as well.

> P.S.  1981 Genesis album!

 +1 bonus.

Barry



More information about the R-help mailing list