R:grepl 以错误的顺序输出匹配项

R: grepl outputs matches in the wrong order

我有一个包含 1 个变量和 5,000 行的数据框(称为测试),其中每个元素都是一个字符串。

1. "Am open about my feelings."                   
2. "Take charge."                                 
3. "Talk to a lot of different people at parties."
4. "Make friends easily."                         
5. "Never at a loss for words."                   
6. "Don't talk a lot."                            
7. "Keep in the background."                      
  .....
5000. "Speak softly."           

我正在查找并输出 3 个特定字符元素的行位置。在这种情况下 df 对象:"Speak softly."、"Take charge."、"Don't talk a lot."

我希望得到以下输出;

[1] 5000 2 6 

但是,出于某种原因,我当前使用的代码会按升序输出行索引,而不是按照上面的项目排序的索引

which(grepl(paste(df, collapse = "|"), test[,1])) 

[1] 2 6 5000 

我真的不确定为什么会这样。我尝试将基于 grepl 的选项(即 FIXED 或 PERL)设置为 TRUE,希望它会改变结果,但事实并非如此。我还尝试搜索一个通用的 'reorder' 函数,但它做的事情与这里需要的完全不同。最后,我尝试删除 which 语句,但它只是将输出更改为二进制并生成 TRUE、FALSE 类型的输出。

编辑

感谢大家帮忙解答。

lapply(big7 , function(p) {
grep(pattern = p, test[ , 1])} ) # correct order of indices  

lapply(big7 , function(p) {
grepl(pattern = p, test[ , 1])} ) #  TRUE/FALSE for each item in the correct order 

试试这个(出于我上面评论中的原因(并且因为 grep returns 数字位置):

  sapply( df , function(p) {grep(patt=p, test[ , 1])} )