grep using a character vector with multiple patterns

前端 未结 10 2445
猫巷女王i
猫巷女王i 2020-11-22 04:29

I am trying to use grep to test whether a vector of strings are present in an another vector or not, and to output the values that are present (the matching pat

10条回答
  •  -上瘾入骨i
    2020-11-22 05:09

    To add to Brian Diggs answer.

    another way using grepl will return a data frame containing all your values.

    toMatch <- myfile$Letter
    
    matches <- myfile[grepl(paste(toMatch, collapse="|"), myfile$Letter), ]
    
    matches
    
    Letter Firstname
    1     A1      Alex 
    2     A6      Alex 
    4     A1       Bob 
    5     A9     Chris 
    6     A6     Chris
    

    Maybe a bit cleaner... maybe?

提交回复
热议问题