Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching strings across columns in R

Tags:

r

mapping

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.

like image 708
novembera Avatar asked Oct 29 '09 23:10

novembera


2 Answers

Thanks to Kevin's suggestion to use apply,

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

results in the desired output.

like image 120
novembera Avatar answered Sep 20 '22 14:09

novembera


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.

like image 26
kmm Avatar answered Sep 18 '22 14:09

kmm