R: Can grep() include more than one pattern?

后端 未结 1 1127
梦毁少年i
梦毁少年i 2021-01-16 08:11

For instance, in this example, I would like to remove the elements in text that contain http and america.

> text &l         


        
相关标签:
1条回答
  • 2021-01-16 08:26

    Generally, as @akrun said:

    text <- c("One word@", "112a httpSentenceamerica", "you and meamerica", "three two one")
    pattern = c("http", "america")
    grep(paste(pattern, collapse = "|"), text, invert = TRUE, value = TRUE)
    # [1] "One word@"     "three two one"
    

    You wrote that your list of words is "long." This solution doesn't scale indefinitely, unsurprisingly:

    long_pattern = paste(rep(pattern, 1300), collapse = "|")
    nchar(long_pattern)
    # [1] 16899
    grep(long_pattern, text, invert = TRUE, value = TRUE)
    # Error in grep(long_pattern, text, invert = TRUE, value = TRUE) :
    

    But if necessary, you could MapReduce, starting with something along the lines of:

    text[Reduce(`&`, Map(function(p) !grepl(p, text), long_pattern))]
    # [1] "One word@"     "three two one"
    
    0 讨论(0)
提交回复
热议问题