开发者

Matching strings across columns in R

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜