Re: [R] find a sequence of characters in a vector
I think I would use Sundar's solution but here are a couple more just in case: > x <- c("a", "z", "e") > y <- c("a", "z", "e", "r", "t", "a", "z", "a", "z", "e", "c") > > #1 > which(apply(embed(y, 3), 1, identical, rev(x))) [1] 1 8 > #2 > library(zoo) > which(rollapply(zoo(y), 3, identical, x, align = "left")) [1] 1 8 On Fri, Jun 5, 2009 at 9:30 AM, Sundar Dorai-Raj wrote: > use gregexpr and paste > >> aze <- paste(c("a", "z", "e"), collapse = "") >> sequence <- paste(c("a","z","e","r","t","a","z","a","z","e","c"), collapse = >> "") >> gregexpr(aze, sequence, fixed = TRUE) > [[1]] > [1] 1 8 > attr(,"match.length") > [1] 3 3 > > HTH, > > --sundar > > On Fri, Jun 5, 2009 at 6:22 AM, Ptit_Bleu wrote: >> >> Hello, >> >> I'm just looking for an easy way to find the positions of a complete >> sequence in a bigger vector. >> For example : >> c("a","z","e") in c("a","z","e","r","t","a","z","a","z","e","c") >> and the result should be >> 1 8 >> that is the positions of the beginning of the complete sequence. >> >> I tried with %in%, match, is.element but all I get is, for example >> which(c("a","z","e") in c("a","z","e","r","t","a","z","a","z","e","c")) >> 1 2 3 >> meaning that each character is in the bigger vector. >> >> It must be easy, except for me. Sorry. >> >> If you have a solution, thanks in advance to share it. >> Have a good week-end, >> Ptit Bleu. >> >> -- >> View this message in context: >> http://www.nabble.com/find-a-sequence-of-characters-in-a-vector-tp23888063p23888063.html >> Sent from the R help mailing list archive at Nabble.com. >> >> __ >> 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. > __ 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.
Re: [R] find a sequence of characters in a vector
Here is one approach (this could be wrapped into a function if you are applying it repeatedly): > one <- c("a","z","e") > two <- c("a","z","e","r","t","a","z","a","z","e","c") > > which( sapply( 1:(length(two)-length(one)+1), + function(i) isTRUE( all.equal( one, two[ i + 0:(length(one)-1) ] ) ) ) ) [1] 1 8 > Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > project.org] On Behalf Of Ptit_Bleu > Sent: Friday, June 05, 2009 7:22 AM > To: r-help@r-project.org > Subject: [R] find a sequence of characters in a vector > > > Hello, > > I'm just looking for an easy way to find the positions of a complete > sequence in a bigger vector. > For example : > c("a","z","e") in c("a","z","e","r","t","a","z","a","z","e","c") > and the result should be > 1 8 > that is the positions of the beginning of the complete sequence. > > I tried with %in%, match, is.element but all I get is, for example > which(c("a","z","e") in c("a","z","e","r","t","a","z","a","z","e","c")) > 1 2 3 > meaning that each character is in the bigger vector. > > It must be easy, except for me. Sorry. > > If you have a solution, thanks in advance to share it. > Have a good week-end, > Ptit Bleu. > > -- > View this message in context: http://www.nabble.com/find-a-sequence-of- > characters-in-a-vector-tp23888063p23888063.html > Sent from the R help mailing list archive at Nabble.com. > > __ > 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.
Re: [R] find a sequence of characters in a vector
use gregexpr and paste > aze <- paste(c("a", "z", "e"), collapse = "") > sequence <- paste(c("a","z","e","r","t","a","z","a","z","e","c"), collapse = > "") > gregexpr(aze, sequence, fixed = TRUE) [[1]] [1] 1 8 attr(,"match.length") [1] 3 3 HTH, --sundar On Fri, Jun 5, 2009 at 6:22 AM, Ptit_Bleu wrote: > > Hello, > > I'm just looking for an easy way to find the positions of a complete > sequence in a bigger vector. > For example : > c("a","z","e") in c("a","z","e","r","t","a","z","a","z","e","c") > and the result should be > 1 8 > that is the positions of the beginning of the complete sequence. > > I tried with %in%, match, is.element but all I get is, for example > which(c("a","z","e") in c("a","z","e","r","t","a","z","a","z","e","c")) > 1 2 3 > meaning that each character is in the bigger vector. > > It must be easy, except for me. Sorry. > > If you have a solution, thanks in advance to share it. > Have a good week-end, > Ptit Bleu. > > -- > View this message in context: > http://www.nabble.com/find-a-sequence-of-characters-in-a-vector-tp23888063p23888063.html > Sent from the R help mailing list archive at Nabble.com. > > __ > 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.
[R] find a sequence of characters in a vector
Hello, I'm just looking for an easy way to find the positions of a complete sequence in a bigger vector. For example : c("a","z","e") in c("a","z","e","r","t","a","z","a","z","e","c") and the result should be 1 8 that is the positions of the beginning of the complete sequence. I tried with %in%, match, is.element but all I get is, for example which(c("a","z","e") in c("a","z","e","r","t","a","z","a","z","e","c")) 1 2 3 meaning that each character is in the bigger vector. It must be easy, except for me. Sorry. If you have a solution, thanks in advance to share it. Have a good week-end, Ptit Bleu. -- View this message in context: http://www.nabble.com/find-a-sequence-of-characters-in-a-vector-tp23888063p23888063.html Sent from the R help mailing list archive at Nabble.com. __ 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.