[R] About grep
Hi,everyone. I have a problem when using the grep. for example: a <- c("aa","aba","abac") b<- c("ab","aba") I want to match the whole word,so grep("^aba$",a) it returns 2 but when I used it a more useful way: grep("^b[2]$",a), it doesn't work at all, it can't find it, returning integer(0). How can I chang the format in the second way? Thanks. -- Shao [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] About grep
try grep(paste("^",b[2],"$",sep=""),a) your version will match "b2": > grep("^b[2]$",c("b","b2","b3")) [1] 2 --- Shao <[EMAIL PROTECTED]> wrote: > Hi,everyone. > > I have a problem when using the grep. > for example: > a <- c("aa","aba","abac") > b<- c("ab","aba") > > I want to match the whole word,so > grep("^aba$",a) > it returns 2 > > but when I used it a more useful way: > grep("^b[2]$",a), > it doesn't work at all, it can't find it, returning integer(0). > > How can I chang the format in the second way? > > Thanks. > > -- > Shao > > [[alternative HTML version deleted]] > > __ > R-help@stat.math.ethz.ch 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. > Pinpoint customers who are looking for what you sell. __ R-help@stat.math.ethz.ch 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] About grep
Oh,great. Thanks. On 8/7/07, Stephen Tucker <[EMAIL PROTECTED]> wrote: > > try > > grep(paste("^",b[2],"$",sep=""),a) > > > your version will match "b2": > > > grep("^b[2]$",c("b","b2","b3")) > [1] 2 > > > --- Shao <[EMAIL PROTECTED]> wrote: > > > Hi,everyone. > > > > I have a problem when using the grep. > > for example: > > a <- c("aa","aba","abac") > > b<- c("ab","aba") > > > > I want to match the whole word,so > > grep("^aba$",a) > > it returns 2 > > > > but when I used it a more useful way: > > grep("^b[2]$",a), > > it doesn't work at all, it can't find it, returning integer(0). > > > > How can I chang the format in the second way? > > > > Thanks. > > > > -- > > Shao > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@stat.math.ethz.ch 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. > > > > > > > > > Pinpoint customers who are looking for what you sell. > http://searchmarketing.yahoo.com/ > -- Shao [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] About grep
Shao wrote: > > Hi,everyone. > > I have a problem when using the grep. > for example: > a <- c("aa","aba","abac") > b<- c("ab","aba") > > I want to match the whole word,so > grep("^aba$",a) > it returns 2 > > but when I used it a more useful way: > grep("^b[2]$",a), > it doesn't work at all, it can't find it, returning integer(0). > > How can I chang the format in the second way? > > Thanks. > The regexp "^b[2]$" matches only two words: "b2" and "b" (in your present grep call with defaults extended = TRUE, perl = FALSE, fixed = FALSE). They don't present in a, that's why grep returns integer(0). If you want to find the word "b[2]" (the expression similar to an array indexing operator), either use fixed=TRUE and remove any regexp markup (^ and $) > a<- c("aa","aba","abac","b[2]") > grep("b[2]",a,fixed=TRUE) [1] 4 , or use escapes > grep("^b\\[2\\]$",a) [1] 4 -- View this message in context: http://www.nabble.com/About-grep-tf4228021.html#a12029243 Sent from the R help mailing list archive at Nabble.com. __ R-help@stat.math.ethz.ch 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] About grep
On 07-Aug-07 04:20:27, Shao wrote: > Hi,everyone. > > I have a problem when using the grep. > for example: > a <- c("aa","aba","abac") > b<- c("ab","aba") > > I want to match the whole word,so > grep("^aba$",a) > it returns 2 > > but when I used it a more useful way: > grep("^b[2]$",a), > it doesn't work at all, it can't find it, returning integer(0). > > How can I chang the format in the second way? > > Thanks. > > -- > Shao The problem is that in the string "^b[2]$" the element b[2] of b is not evaluated, but simply the successive characters ^ b [ 2 ] $ are passed to grep as a character string, which of course is not found. You can construct a character string with the value of b[2] in it by using paste(): grep(paste("^",b[2],"$",sep=""),a) [1] 2 Ted. E-Mail: (Ted Harding) <[EMAIL PROTECTED]> Fax-to-email: +44 (0)870 094 0861 Date: 07-Aug-07 Time: 07:43:14 -- XFMail -- __ R-help@stat.math.ethz.ch 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.