tidyverse / dplyr

dplyr: A grammar of data manipulation
https://dplyr.tidyverse.org/
Other
4.77k stars 2.12k forks source link

It seems dplyr::filter only consider the first if_any when numerous if_any were used in a same dplyr::filter #5838

Closed Yunuuuu closed 3 years ago

Yunuuuu commented 3 years ago
library(dplyr)
library(stringr)
starwars %>%
  dplyr::filter(
    if_any(.fns = ~str_detect(., "(?i)blue"))
  ) # A tibble: 28 x 14
starwars %>%
  dplyr::filter(
    if_any(.fns = ~str_detect(., "(?i)none"))
  ) # A tibble: 40 x 14
starwars %>%
  dplyr::filter(
    if_any(.fns = ~str_detect(., "(?i)none")),
    if_any(.fns = ~str_detect(., "(?i)blue"))
  ) # A tibble: 40 x 14
starwars %>%
  dplyr::filter(
    if_any(.fns = ~str_detect(., "(?i)none")) &
    if_any(.fns = ~str_detect(., "(?i)blue"))
  ) # A tibble: 40 x 14
starwars %>%
  dplyr::filter(
    if_any(.fns = ~str_detect(., "(?i)blue")),
    if_any(.fns = ~str_detect(., "(?i)none"))
  ) # A tibble: 28 x 14
starwars %>%
  dplyr::filter(
    if_any(.fns = ~str_detect(., "(?i)blue")) &
    if_any(.fns = ~str_detect(., "(?i)none"))
  ) # A tibble: 28 x 14

annotation behind # is the row number * column number

dplyr::filter will ignore the second if_any, even when we use a function & to connect the both if_any.

here is my sessioninfo:

R version 4.0.5 (2021-03-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stringr_1.4.0 dplyr_1.0.5  

loaded via a namespace (and not attached):
 [1] fansi_0.4.2      assertthat_0.2.1 utf8_1.2.1       crayon_1.4.1    
 [5] R6_2.5.0         DBI_1.1.1        lifecycle_1.0.0  magrittr_2.0.1  
 [9] pillar_1.5.1     cli_2.3.1        stringi_1.5.3    rlang_0.4.10    
[13] rstudioapi_0.13  vctrs_0.3.7      generics_0.1.0   ellipsis_0.3.1  
[17] tools_4.0.5      glue_1.4.2       purrr_0.3.4      compiler_4.0.5  
[21] pkgconfig_2.0.3  tidyselect_1.1.0 tibble_3.1.0    
romainfrancois commented 3 years ago

Fixed by https://github.com/tidyverse/dplyr/pull/5793 and/or https://github.com/tidyverse/dplyr/pull/5835