Filter a column which contains several keywords

前端 未结 5 1267
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, . ) )
    

提交回复
热议问题