Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a logical vector for matches: regexp supported?

Tags:

regex

r

I want to return a logical vector for regexp matches over a character vector, but match or %in do not seem to support regular expressions, e.g.:

> x <- c("Bill", "Brett", "Jane")
> grep("^B", x)
[1] 1 2
> x %in% "^B"
[1] FALSE FALSE FALSE

I would like this to return

[1] TRUE TRUE FALSE

Ideas?

Thanks,

Roberto

like image 833
Roberto Avatar asked Feb 27 '23 03:02

Roberto


2 Answers

Just chain it:

> seq(along=(x)) %in% grep("^B", x)
[1]  TRUE  TRUE  FALSE

So you could write yourself a little helper function that does both as shown here. But I presume one of the grep() variants does that as well... Ah, yes, grepl() is your friend:

> grepl("^B", x)
[1]  TRUE  TRUE  FALSE

Nothing that a quick help(grep) can't cure ;-)

like image 187
Dirk Eddelbuettel Avatar answered Mar 07 '23 17:03

Dirk Eddelbuettel


One way is just to wrap two simple steps up in a function, where we get grep() to do it's thing, but supply the indices of the elements in x as the LHS of the call to %in%:

foo <- function(x, pattern, ...) {
    seq_along(x) %in% grep(pattern, x, ...) 
}

With your example data we get:

> x <- c("Bill", "Brett", "Jane")
> foo(x, pattern = "^B")
[1]  TRUE  TRUE FALSE
like image 43
Gavin Simpson Avatar answered Mar 07 '23 15:03

Gavin Simpson