[R] matching a sequence in a vector?

Rainer M Krug r.m.krug at gmail.com
Wed Feb 15 10:02:22 CET 2012


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 15/02/12 05:17, Redding, Matthew wrote:
> Hi All,
> 
> 
> I've been trawling through the documentation and listserv archives
> on this topic -- but as yet have not found a solution.  I'm sure
> this is pretty simple with R, but I cannot work out how without 
> resorting to ugly nested loops.

Just another idea: what about converting the vector to a character vector

exmplstr <- paste(exmpl, collapse="")
patrnstr <- paste(patrn, collapse="")

and then search for patrnstr in exmplstr?

Rainer


> 
> As far as I can tell, grep, match, and %in% are not the correct
> tools.
> 
> Question: given these vectors -- patrn <- c(1,2,3,4) exmpl <-
> c(3,3,4,2,3,1,2,3,4,8,8,23,1,2,3,4,4,34,4,3,2,1,1,2,3,4)
> 
> how do I get the desired answer by finding the occurence of the
> pattern and returning the starting indices: 6, 13, 23
> 
> Suggestions very much appreciated!
> 
> Kind regards,
> 
> 
> 
> 
> Matt Redding, Ph.D. Principal Scientist Geochemist/Soil Chemist 
> Queensland Primary Industries & Fisheries DEEDI PO Box 102,
> Toowoomba, 4350, Qld ph: 0746 881372 fax: 0746 881192
> 
> 
> ********************************DISCLAIMER**************...{{dropped:15}}
>
>  ______________________________________________ 
> R-help at 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.


- -- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :       +33 - (0)9 53 10 27 44
Cell:       +33 - (0)6 85 62 59 98
Fax :       +33 - (0)9 58 10 27 44

Fax (D):    +49 - (0)3 21 21 25 22 44

email:      Rainer at krugs.de

Skype:      RMkrug
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk87dJ4ACgkQoYgNqgF2egojXACcDPXGfqB+8+Cmz80z+plX376y
FX4An1+PWTr3OJceYVCHYxz4y02FZ/ei
=ru9x
-----END PGP SIGNATURE-----



More information about the R-help mailing list