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.
Thanks to Kevin's suggestion to use apply,
>
mapply(grepl,df$letter,df$food)
results in the desired output.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With