grep using a character vector with multiple patterns

前端 未结 10 2320
猫巷女王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: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?

    0 讨论(0)
  • 2020-11-22 05:10

    Good answers, however don't forget about filter() from dplyr:

    patterns <- c("A1", "A9", "A6")
    >your_df
      FirstName Letter
    1      Alex     A1
    2      Alex     A6
    3      Alex     A7
    4       Bob     A1
    5     Chris     A9
    6     Chris     A6
    
    result <- filter(your_df, grepl(paste(patterns, collapse="|"), Letter))
    
    >result
      FirstName Letter
    1      Alex     A1
    2      Alex     A6
    3       Bob     A1
    4     Chris     A9
    5     Chris     A6
    
    0 讨论(0)
  • 2020-11-22 05:14

    This should work:

    grep(pattern = 'A1|A9|A6', x = myfile$Letter)
    

    Or even more simply:

    library(data.table)
    myfile$Letter %like% 'A1|A9|A6'
    
    0 讨论(0)
  • 2020-11-22 05:22

    Using the sapply

     patterns <- c("A1", "A9", "A6")
             df <- data.frame(name=c("A","Ale","Al","lex","x"),Letters=c("A1","A2","A9","A1","A9"))
    
    
    
       name Letters
    1    A      A1
    2  Ale      A2
    3   Al      A9
    4  lex      A1
    5    x      A9
    
    
     df[unlist(sapply(patterns, grep, df$Letters, USE.NAMES = F)), ]
      name Letters
    1    A      A1
    4  lex      A1
    3   Al      A9
    5    x      A9
    
    0 讨论(0)
  • 2020-11-22 05:24

    Take away the spaces. So do:

    matches <- unique(grep("A1|A9|A6", myfile$Letter, value=TRUE, fixed=TRUE))
    
    0 讨论(0)
  • 2020-11-22 05:25

    Not sure whether this answer has already appeared...

    For the particular pattern in the question, you can just do it with a single grep() call,

    grep("A[169]", myfile$Letter)
    
    0 讨论(0)
提交回复
热议问题