tags:

views:

49

answers:

3

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

+4  A: 

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 ;-)

Dirk Eddelbuettel
@comment : two times we post simultaneously today :-) You're really lurking the questions, no? I deleted my answer, as you added that to your post.
Joris Meys
@Dirk @Joris: wow, the same three answers inside of one minute... I deleted mine as well.
Joshua Ulrich
@Joshua : seems like we need more questions to fulfill the answering needs of us. Maybe we should mention the existence of this site on the mailing list of R as well. I guess they won't mind losing a bit of traffic there...
Joris Meys
@Joris, perhaps but you have to ask yourself if you *really* want some of that traffic. ;-)
Joshua Ulrich
@Joshua : hmmm... good point there...
Joris Meys
A: 

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
Gavin Simpson
+1  A: 

Use grepl:

> x <- c("Bill", "Brett", "Jane") 
> grepl("^B", x) 
[1]  TRUE  TRUE FALSE

Edit

Ooops, seems Dirk already had this further down his answer. For the sake of novelty, here's some other ways:

> ans <- rep(FALSE,length(x))
> ans[grep("^B",x)]<-TRUE
> ans
[1]  TRUE  TRUE FALSE


> 1:length(x) %in% grep("^B",x)
[1]  TRUE  TRUE FALSE
James