简体   繁体   English

Dplyr 过滤多个相似条件

[英]Dplyr Filter Multiple Like Conditions

I am trying to do a filter in dplyr where a column is like certain observations.我试图在 dplyr 中做一个过滤器,其中一列就像某些观察。 I can use sqldf as我可以使用 sqldf 作为

Test <- sqldf("select * from database 
          Where SOURCE LIKE '%ALPHA%' 
          OR SOURCE LIKE '%BETA%' 
          OR SOURCE LIKE '%GAMMA%'")

I tried to use the following which doesn't return any results:我尝试使用以下不返回任何结果的方法:

database %>% dplyr::filter(SOURCE %like% c('%ALPHA%', '%BETA%', '%GAMMA%'))

Thanks谢谢

You can use grepl with ALPHA|BETA|GAMMA , which will match if any of the three patterns is contained in SOURCE column. 可以将greplALPHA|BETA|GAMMA ,如果SOURCE列中包含三个模式中的任何一个,则它将匹配。

database %>% filter(grepl('ALPHA|BETA|GAMMA', SOURCE))

If you want it to be case insensitive, add ignore.case = T in grepl . 如果希望它不区分大小写,请在grepl添加ignore.case = T

%like% is from the data.table package. %like%来自data.table包。 You're probably also seeing this warning message: 您可能还会看到以下警告消息:

Warning message:
In grepl(pattern, vector) :
  argument 'pattern' has length > 1 and only the first element will be used

The %like% operator is just a wrapper around the grepl function, which does string matching using regular expressions. %like%运算符只是grepl函数的包装,该函数使用正则表达式进行字符串匹配。 So % aren't necessary, and in fact they represent literal percent signs. 因此, %是不必要的,实际上它们代表了实际的百分号。

You can only supply one pattern to match at a time, so either combine them using the regex 'ALPHA|BETA|GAMMA' (as Psidom suggests) or break the tests into three statements: 您一次只能提供一种模式来进行匹配,因此可以使用正则表达式'ALPHA|BETA|GAMMA' (如Psidom所建议的)将它们组合在一起,或者将测试分为三个语句:

database %>%
  dplyr::filter(
    SOURCE %like% 'ALPHA' |
      SOURCE %like% 'BETA' |
      SOURCE %like% 'GAMMA'
    )

Building on Psidom and Nathan Werth's response, for a Tidyverse friendly and concise method, we can do;基于 Psidom 和 Nathan Werth 的回应,对于 Tidyverse 友好简洁的方法,我们可以做到;

library(data.table); library(tidyverse)
database %>%
  dplyr::filter(SOURCE %ilike% "ALPHA|BETA|GAMMA") # ilike = case insensitive fuzzysearch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM