tags:

views:

97

answers:

2

I've got a data frame with 2 character columns. I'd like to find the rows which one column contains the other, however grepl is being strange. Any ideas?

> ( df <- data.frame(letter=c('a','b'),food = c('apple','pear','bun','beets')) )
  letter  food
1      a apple
2      b  pear
3      a   bun
4      b beets 

> grepl(df$letter,df$food)

[1]  TRUE  TRUE FALSE FALSE

but i want T F F T

Thanks.

+1  A: 

When I run your code, I get a warning:

Warning message:
In grepl(df$letter, df$food) :
  argument 'pattern' has length > 1 and only the first element will be used

This is confirmed by ?grepl under pattern:

If a character vector of length 2 or more is supplied, 
the first element is used with a warning.

So grepl is finding the a in both apple and pear. This doesn't solve your problem (apply or one of its variants?), but it does explain the output you are getting.

Kevin
Thanks. Apparently the warning is new in R version 2.10.0.
novembera
I'm using: R version 2.10.0 Patched (2009-10-28 r50254) x86_64-apple-darwin9.8.0
Kevin
A: 

Thanks to Kevin's suggestion to use apply,

> mapply(grepl,df$letter,df$food)

results in the desired output.

novembera