Filter a column which contains several keywords

前端 未结 5 1268
Happy的楠姐
Happy的楠姐 2020-12-12 01:53

I am trying to filter a column which contains several keywords (in this example dog and cat) but I am having problems as only the first element is being used.



        
相关标签:
5条回答
  • 2020-12-12 02:14

    Here is a dplyr method:

    library(stringi)
    library(dplyr)
    
    data = data_frame(
      id = c(1:7),
      type = c("dog1","dog2" ,"cat1","cat2","zebra1", "parrot5", "elephant15")
    )
    
    
    data %>%
      filter(animals %>%
               paste(collapse = "|") %>%
               stri_detect_regex(type, . ) )
    
    0 讨论(0)
  • 2020-12-12 02:17

    Like @Tgsmith61591 mentioned, the pattern argument for the grep function requires a string. Since you're passing in a vector it's warning you that it will only process the first element.

    Another solution would be something like this:

    dfilter <- unique(grep(paste(filter1, collapse = "|"), df1$type, value=TRUE))
    

    See this post grep using a character vector with multiple patterns

    0 讨论(0)
  • 2020-12-12 02:21

    Try this:

    dfilter <- df1[sapply(filter1, function(x) grep(x,df1$type)),]
    

    It's complaining because your filter is a vector and grep wants a string.

    Edit:

    From this answer:

    dfilter <- df1[df1$type %in% grep(paste(filter1, collapse="|"), df1$type, value=TRUE), ]
    
    0 讨论(0)
  • 2020-12-12 02:22
    df1[(gsub('\\d','',df1$type) %in% filter1),]
      id type
    1  1 dog1
    2  2 dog2
    3  3 cat1
    4  4 cat2
    
    0 讨论(0)
  • 2020-12-12 02:32

    grep can use | as an or, so why not paste your filters together with | as a separator:

    dfilter <- df1[grep(paste0(filter1, collapse = "|"), df1$type),]
    
    0 讨论(0)
提交回复
热议问题