Hi Folks, Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern? Or is that a ridiculous question, since I'm trying to find something based on a pattern that doesn't exist? test <- c("x1", "x2", "x3", "x1 + x2 + x3") test [1] "x1" "x2" "x3" "x1 + x2 + x3" grep("x1 + x2", test, fixed=TRUE, value = TRUE) [1] "x1 + x2 + x3" But what if only have "x1 + x3" as the pattern and still want to return "x1 + x2 + x3"? grep("x1 + x3", test, fixed=TRUE, value = TRUE) character(0) I'm sure this looks like an odd question. I'm trying to build a function and stuck on this. Rather than dropping the whole function on the list, I thought I'd try one piece I needed help with...although I suspect that this question itself probably does bode well for my function :) Thanks! Joe
Hi Joe, This may help you: test <- c("x1", "x2", "x3", "x1 + x2 + x3") multigrep<-function(x1,x2) { xbits<-unlist(strsplit(x1," ")) nbits<-length(xbits) xans<-rep(FALSE,nbits) for(i in 1:nbits) if(length(grep(xbits[i],x2))) xans[i]<-TRUE return(all(xans)) } multigrep("x1 + x3","x1 + x2 + x3") [1] TRUE multigrep("x1 + x4","x1 + x2 + x3") [1] FALSE Jim On Wed, Mar 22, 2017 at 10:50 AM, Joe Ceradini <joeceradini at gmail.com> wrote:> Hi Folks, > > Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern? > Or is that a ridiculous question, since I'm trying to find something > based on a pattern that doesn't exist? > > test <- c("x1", "x2", "x3", "x1 + x2 + x3") > test > [1] "x1" "x2" "x3" "x1 + x2 + x3" > > grep("x1 + x2", test, fixed=TRUE, value = TRUE) > [1] "x1 + x2 + x3" > > > But what if only have "x1 + x3" as the pattern and still want to > return "x1 + x2 + x3"? > > grep("x1 + x3", test, fixed=TRUE, value = TRUE) > character(0) > > I'm sure this looks like an odd question. I'm trying to build a > function and stuck on this. Rather than dropping the whole function on > the list, I thought I'd try one piece I needed help with...although I > suspect that this question itself probably does bode well for my > function :) > > Thanks! > Joe > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
It is not clear to me what you mean, but:> grep ("x1 \\+.* \\+ x3",test, value = TRUE)[1] "x1 + x2 + x3" ## This will miss "x1 + x3" though. seems to do what you want, maybe. Perhaps you need to read up about regular expressions and/or clarify what you want to do. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Tue, Mar 21, 2017 at 4:50 PM, Joe Ceradini <joeceradini at gmail.com> wrote:> Hi Folks, > > Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern? > Or is that a ridiculous question, since I'm trying to find something > based on a pattern that doesn't exist? > > test <- c("x1", "x2", "x3", "x1 + x2 + x3") > test > [1] "x1" "x2" "x3" "x1 + x2 + x3" > > grep("x1 + x2", test, fixed=TRUE, value = TRUE) > [1] "x1 + x2 + x3" > > > But what if only have "x1 + x3" as the pattern and still want to > return "x1 + x2 + x3"? > > grep("x1 + x3", test, fixed=TRUE, value = TRUE) > character(0) > > I'm sure this looks like an odd question. I'm trying to build a > function and stuck on this. Rather than dropping the whole function on > the list, I thought I'd try one piece I needed help with...although I > suspect that this question itself probably does bode well for my > function :) > > Thanks! > Joe > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
> On Mar 21, 2017, at 6:31 PM, Bert Gunter <bgunter.4567 at gmail.com> wrote: > > It is not clear to me what you mean, but: > >> grep ("x1 \\+.* \\+ x3",test, value = TRUE) > [1] "x1 + x2 + x3" > > ## This will miss "x1 + x3" though.So then this might be acceptable: grep ("x1\\ \\+.* x3", test, value = TRUE)> > seems to do what you want, maybe. Perhaps you need to read up about > regular expressions and/or clarify what you want to do. > > > Cheers, > Bert > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Tue, Mar 21, 2017 at 4:50 PM, Joe Ceradini <joeceradini at gmail.com> wrote: >> Hi Folks, >> >> Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern? >> Or is that a ridiculous question, since I'm trying to find something >> based on a pattern that doesn't exist? >> >> test <- c("x1", "x2", "x3", "x1 + x2 + x3") >> test >> [1] "x1" "x2" "x3" "x1 + x2 + x3" >> >> grep("x1 + x2", test, fixed=TRUE, value = TRUE) >> [1] "x1 + x2 + x3" >> >> >> But what if only have "x1 + x3" as the pattern and still want to >> return "x1 + x2 + x3"? >> >> grep("x1 + x3", test, fixed=TRUE, value = TRUE) >> character(0) >> >> I'm sure this looks like an odd question. I'm trying to build a >> function and stuck on this. Rather than dropping the whole function on >> the list, I thought I'd try one piece I needed help with...although I >> suspect that this question itself probably does bode well for my >> function :) >> >> Thanks! >> Joe >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.David Winsemius Alameda, CA, USA
Hi Joe, you could also rethink your pattern: grep("x1 \\+ x2", test, value = TRUE) grep("x1 \\+ x", test, value = TRUE) grep("x1 \\+ x[0-9]", test, value = TRUE) HTH Ulrik On Wed, 22 Mar 2017 at 02:10 Jim Lemon <drjimlemon at gmail.com> wrote:> Hi Joe, > This may help you: > > test <- c("x1", "x2", "x3", "x1 + x2 + x3") > multigrep<-function(x1,x2) { > xbits<-unlist(strsplit(x1," ")) > nbits<-length(xbits) > xans<-rep(FALSE,nbits) > for(i in 1:nbits) if(length(grep(xbits[i],x2))) xans[i]<-TRUE > return(all(xans)) > } > multigrep("x1 + x3","x1 + x2 + x3") > [1] TRUE > multigrep("x1 + x4","x1 + x2 + x3") > [1] FALSE > > Jim > > On Wed, Mar 22, 2017 at 10:50 AM, Joe Ceradini <joeceradini at gmail.com> > wrote: > > Hi Folks, > > > > Is there a way to find "x1 + x2 + x3" given "x1 + x3" as the pattern? > > Or is that a ridiculous question, since I'm trying to find something > > based on a pattern that doesn't exist? > > > > test <- c("x1", "x2", "x3", "x1 + x2 + x3") > > test > > [1] "x1" "x2" "x3" "x1 + x2 + x3" > > > > grep("x1 + x2", test, fixed=TRUE, value = TRUE) > > [1] "x1 + x2 + x3" > > > > > > But what if only have "x1 + x3" as the pattern and still want to > > return "x1 + x2 + x3"? > > > > grep("x1 + x3", test, fixed=TRUE, value = TRUE) > > character(0) > > > > I'm sure this looks like an odd question. I'm trying to build a > > function and stuck on this. Rather than dropping the whole function on > > the list, I thought I'd try one piece I needed help with...although I > > suspect that this question itself probably does bode well for my > > function :) > > > > Thanks! > > Joe > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]