2009-10-29 78 views
5

我有一个包含2个字符列的数据框。我想找到一列中包含另一列的行,但是grepl很奇怪。有任何想法吗?R中的列匹配字符串

> (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 

,但我想五六F T

感谢。

回答

5

感谢凯文的建议,使用申请,

> mapply(grepl,DF $信,DF $食品)

结果所需的输出。

2

当我运行代码,我得到一个警告:

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

这是由?grepl确认下pattern

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

所以grepl是找到一两个苹果和梨。这不能解决你的问题(应用或其变体之一?),但它解释了你得到的输出。

+0

谢谢。显然这个警告在R 2.10.0版中是新的。 – novembera 2009-10-30 00:16:57

+0

我正在使用: R版本2.10.0已修补(2009-10-28 r50254) x86_64-apple-darwin9.8.0 – kmm 2009-10-30 00:19:00