[R] matching a sequence in a vector?

Rainer M Krug r.m.krug at gmail.com
Wed Feb 15 09:40:29 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.

No actual solution - but this sounds to me like a "moving window"
statistic. I googled for "moving window R" and found, among others,
the following:

http://tolstoy.newcastle.edu.au/R/help/04/10/5161.html

Maybe this can give you some additional ideas - for your question, you
would not calculate the e.g. mean of the moving window, but check if
the sequence in the window is equal to the one you are looking for.

Cheers,

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/

iEYEARECAAYFAk87b3wACgkQoYgNqgF2egpqxACeNIMFFIDM6oqyejLR5yewNz2W
R2AAn1elVRr0zqbADRFyZupWnMirAuZy
=BXd9
-----END PGP SIGNATURE-----



More information about the R-help mailing list