grep using a character vector with multiple patterns

前端 未结 10 2453
猫巷女王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条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 05:28

    In addition to @Marek's comment about not including fixed==TRUE, you also need to not have the spaces in your regular expression. It should be "A1|A9|A6".

    You also mention that there are lots of patterns. Assuming that they are in a vector

    toMatch <- c("A1", "A9", "A6")
    

    Then you can create your regular expression directly using paste and collapse = "|".

    matches <- unique (grep(paste(toMatch,collapse="|"), 
                            myfile$Letter, value=TRUE))
    

提交回复
热议问题