[R] pattern search
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Oct 29 21:05:25 CEST 2004
On Fri, 29 Oct 2004, Sean Liang wrote:
> Thanks for getting back to me. My situation is a bit more complicated.
> I have a vector of sequences each of which might contain any number of a
> given pattern (e.g.
>
> >pat=c("ATCGTTTGCTAC", "GGCTAATGCATTGC");
> > grep ("TGC", pat)
> [1] 1 2
>
> grep only tells me the position of first occurrence in each element
> whereas the second element contains two "TGC"s.
sapply(strsplit(paste(1,pat,2, sep=""), "TGC"), length)-1
and you can also figure out the positions.
You can also do it recursively with regexpr (on the same page as grep, no
less).
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list