Re: [R] Grep command

2016-05-19 Thread Joyce Robbins
I'm not sure you need grep: > all %in% some [1] TRUE FALSE TRUE FALSE FALSE TRUE On Thu, May 19, 2016 at 7:58 PM, MacQueen, Don wrote: > Start with: > > > all <- c("ants","birds","cats","dogs","elks","fox") > > all[grep('ants|cats|fox',all)] > [1] "ants" "cats" "fox" > > Then construct the f

Re: [R] Grep command

2016-05-19 Thread MacQueen, Don
Start with: > all <- c("ants","birds","cats","dogs","elks","fox") > all[grep('ants|cats|fox',all)] [1] "ants" "cats" "fox" Then construct the first arg to grep: > some <- c("ants","cats","fox") > all[ grep( paste(some,collapse='|') , all)] [1] "ants" "cats" "fox" -- Don MacQueen Lawrence Li

Re: [R] Grep command

2016-05-19 Thread David Winsemius
> On May 19, 2016, at 4:09 PM, Steven Yen wrote: > > What is a good way to grep multiple strings (say in a vector)? In the > following, I grep ants, cats, and fox separately and concatenate them, > is there a way to grep the trio in one action? Thanks. > > all<-c("ants","birds","cats","dogs",

Re: [R] Grep command

2016-05-19 Thread Peter Langfelder
I use my own functions multiGrep and multiGrepl: multiGrep = function(patterns, x, ..., sort = TRUE, invert = FALSE) { if (invert) { out = multiIntersect(lapply(patterns, grep, x, ..., invert = TRUE)) } else out = unique(unlist(lapply(patterns, grep, x, ..., invert = FALSE))); if (

[R] Grep command

2016-05-19 Thread Steven Yen
What is a good way to grep multiple strings (say in a vector)? In the following, I grep ants, cats, and fox separately and concatenate them, is there a way to grep the trio in one action? Thanks. all<-c("ants","birds","cats","dogs","elks","fox"); all [1] "ants" "birds" "cats" "dogs" "elks" "

Re: [R] Grep command

2016-05-04 Thread William Dunlap via R-help
No matter how expert you are at writing regular expressions, it is important to list which sorts of strings you want matched and which you do not want matched. Saying you want to match "age" but not "age2" leads to lots of possibilities. Saying how you want to categorize each string in a vector o

Re: [R] Grep command

2016-05-04 Thread David Winsemius
> On May 3, 2016, at 11:16 PM, Jeff Newmiller wrote: > > Yes, but the answer is likely to depend on the actual patterns of strings in > your real data, so the sooner you go find a book or tutorial on regular > expressions the better. This is decidedly not R specific and there are > already l

Re: [R] Grep command

2016-05-04 Thread Doran, Harold
You asked this question yesterday, and received responses on this same response. Is there a reason this is reposted? -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Steven Yen Sent: Wednesday, May 04, 2016 1:46 AM To: r-help Subject: [R] Grep command

Re: [R] Grep command

2016-05-04 Thread Niels Jespersen
> x <- c("abc","def","rst","xyz","age","age2") > grep("^age$", x) [1] 5 > grep("^age2$", x) [1] 6 > > -Oprindelig meddelelse- Fra: R-help [mailto:r-help-boun...@r-project.org] På vegne af St

Re: [R] Grep command

2016-05-04 Thread Jeff Newmiller
Yes, but the answer is likely to depend on the actual patterns of strings in your real data, so the sooner you go find a book or tutorial on regular expressions the better. This is decidedly not R specific and there are already lots of resources out there. Given the example you provide, the p

Re: [R] Grep command

2016-05-03 Thread Omar André Gonzáles Díaz
Hi Steven, grep uses regex... so you can use this: -grep("age$",x): it says: match "a", then "g", then "e" and stop. The "$" menas until here and no more. > grep("age$",x) [1] 5 2016-05-04 1:02 GMT-05:00 Jim Lemon : > Hi Steven, > If this is just a one-off, you could do this: > > grepl("age",

Re: [R] Grep command

2016-05-03 Thread Jim Lemon
Hi Steven, If this is just a one-off, you could do this: grepl("age",x) & nchar(x)<4 returning a logical vector containing TRUE for "age" but not "age2" Jim On Wed, May 4, 2016 at 3:45 PM, Steven Yen wrote: > Dear all > In the grep command below, is there a way to identify only "age" and > no

[R] Grep command

2016-05-03 Thread Steven Yen
Dear all In the grep command below, is there a way to identify only "age" and not "age2"? In other words, I like to greb "age" and "age2" separately, one at a time. Thanks. x<-c("abc","def","rst","xyz","age","age2") x [1] "abc" "def" "rst" "xyz" "age" "age2" grep("age2",x) [1] 6 grep("age

Re: [R] grep command

2016-05-03 Thread Hervé Pagès
On 05/03/2016 06:05 AM, Jeff Newmiller wrote: Isn't that just an inefficient way to do "age" == x Yep, it's an inefficient way to do which(x == "age"). H. ? -- Hervé Pagès Program in Computational Biology Division of Public Health Sciences Fred Hutchinson Cancer Research Center 1100 Fa

Re: [R] grep command

2016-05-03 Thread Jeff Newmiller
Isn't that just an inefficient way to do "age" == x ? -- Sent from my phone. Please excuse my brevity. On May 3, 2016 3:57:05 AM PDT, Ivan Calandra wrote: >What about? > >grep("^age$", x) > >Ivan > >-- >Ivan Calandra, PhD >Scientific Mediator >University of Reims Champagne-Ardenne >GEGENAA -

Re: [R] grep command

2016-05-03 Thread Ivan Calandra
Oh, and regarding the moderator approval, I guess it's because you're a new user to the list. Ivan -- Ivan Calandra, PhD Scientific Mediator University of Reims Champagne-Ardenne GEGENAA - EA 3795 CREA - 2 esplanade Roland Garros 51100 Reims, France +33(0)3 26 77 36 89 ivan.calan...@univ-reims.

Re: [R] grep command

2016-05-03 Thread Ivan Calandra
What about? grep("^age$", x) Ivan -- Ivan Calandra, PhD Scientific Mediator University of Reims Champagne-Ardenne GEGENAA - EA 3795 CREA - 2 esplanade Roland Garros 51100 Reims, France +33(0)3 26 77 36 89 ivan.calan...@univ-reims.fr -- https://www.researchgate.net/profile/Ivan_Calandra https://

[R] grep command

2016-05-03 Thread Steven Yen
Dear all In the grep command below, is there a way to identify only "age" and not "age2"? Thanks. > x<-c("abc","def","rst","xyz","age","age2") > x [1] "abc" "def" "rst" "xyz" "age" "age2" > grep("age2",x) [1] 6 > grep("age",x) # I need to grab "age" only, not "age2" [1] 5 6 Also, I post mess