How to replace retired `all_vars()` and `any_vars()` using across?

Hi tidyfolks,

the curent {dplyr} devel makes it clear that all scoped variants (e.g. filter_if() below) and functions such as vars(), all_vars(), any_vars() are getting retired. Instead, they suggest to rely on the new adverb across().

For many operations I do find the conversion quite straightforward, but I struggle to convert the code below which filters rows based on whether any/all cells contain a given value.

I can make it work with rowwise(), but I wonder if I did not miss how to use across() properly in this case...

library(tidyverse) ## using current github versions of all key packages
  
tib <- tibble(a = c(0, 0, 1),
              b = c(1, 0, 1),
              c = c(0, 0, 1))

tib
#> # A tibble: 3 x 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     0     1     0
#> 2     0     0     0
#> 3     1     1     1

tib %>%
  filter_if(is.numeric, any_vars(. == 1))
#> # A tibble: 2 x 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     0     1     0
#> 2     1     1     1

# new alternative to any_vars?
tib %>%
  rowwise() %>%
  filter(any(across(is.numeric) == 1))
#> # A tibble: 2 x 3
#> # Rowwise: 
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     0     1     0
#> 2     1     1     1

tib %>%
  filter_if(is.numeric, all_vars(. == 1))
#> # A tibble: 1 x 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     1     1     1

# new alternative to all_vars?
tib %>%
  rowwise() %>%
  filter(all(across(is.numeric) == 1))
#> # A tibble: 1 x 3
#> # Rowwise: 
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     1     1     1

Created on 2020-02-16 by the reprex package (v0.3.0)

Avoiding rowwise() is perhaps important at the light of its current performances. See:

Please let me know!

Alex

With the experimental verb lay https://github.com/romainfrancois/lay

the task becomes straightforward:

library(tidyverse) ## using current github versions of all key packages
library(lay)

tib <- tibble(a = c(0, 0, 1),
              b = c(1, 0, 1),
              c = c(0, 0, 1))

tib
#> # A tibble: 3 x 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     0     1     0
#> 2     0     0     0
#> 3     1     1     1

tib %>%
  filter_if(is.numeric, any_vars(. == 1))
#> # A tibble: 2 x 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     0     1     0
#> 2     1     1     1

## across alternative:
tib %>%
  filter(lay(across(is.numeric), ~ any(.x == 1)))
#> # A tibble: 2 x 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     0     1     0
#> 2     1     1     1


tib %>%
  filter_if(is.numeric, all_vars(. == 1))
#> # A tibble: 1 x 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     1     1     1

## across alternative:
tib %>%
  filter(lay(across(is.numeric), ~ all(.x == 1)))
#> # A tibble: 1 x 3
#>       a     b     c
#>   <dbl> <dbl> <dbl>
#> 1     1     1     1

Created on 2020-02-16 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.