[R] Pattern match

Mark Sharp msharp at txbiomed.org
Wed Mar 20 19:03:27 CET 2013


I like the stringr package. Its functions allow vectors for the patterns. 

>From the examples of str_detect()
> fruit <- c("apple", "banana", "pear", "pinapple")
> str_detect(fruit, "a")
[1] TRUE TRUE TRUE TRUE
> str_detect(fruit, "^a")
[1]  TRUE FALSE FALSE FALSE
> str_detect(fruit, "a$")
[1] FALSE  TRUE FALSE FALSE
> str_detect(fruit, "b")
[1] FALSE  TRUE FALSE FALSE
> str_detect(fruit, "[aeiou]")
[1] TRUE TRUE TRUE TRUE
> 
> # Also vectorised over pattern
> str_detect("aecfg", letters)
 [1]  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[18] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

R. Mark Sharp
msharp at TxBiomed.org




On Mar 20, 2013, at 11:28 AM, Christofer Bogaso wrote:

> Hello again, in the help page of grep() function, it is written that
> 
> pattern:
> 
> character string containing a regular expression (or character string
> for fixed = TRUE) to be matched in the given character vector. Coerced
> by as.character to a character string if possible. If a character
> vector of length 2 or more is supplied, the first element is used with
> a warning. Missing values are allowed except for regexpr and gregexpr.
> 
> But I have a vetcor of length '(> 1)' for the pattern match, and I
> need to have approximate match.
> 
> Is there any function similar to grep() to handle that?
> 
> Thanks for your help.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list