How to do standard evaluation with the new tidyverse?

Since summaries_ is deprecated, what's the new way of doing SE? I tried the following but it does not give the right answer.

x <- "Species"
y <- "Sepal.Length"
iris %>% summarise(n_unique = n_distinct(all_of(y)), .by = all_of(x))

The two have different status. The first one, y is an env-variable in a data masking context, you can use .data[[y]].

The second one, x, is in a tidy-select context, so you typically use all_of().

library(dplyr) |> suppressPackageStartupMessages()
x <- "Species"
y <- "Sepal.Length"

summarise(iris,
          n_unique = n_distinct(Sepal.Length), .by = Species)
#>      Species n_unique
#> 1     setosa       15
#> 2 versicolor       21
#> 3  virginica       21

summarise(iris,
          n_unique = n_distinct(.data[[y]]), .by = all_of(x))
#>      Species n_unique
#> 1     setosa       15
#> 2 versicolor       21
#> 3  virginica       21

Created on 2024-02-13 with reprex v2.0.2

See the programming vignette for explanations, and ?summarise to find out what is data masking or tidy selection.

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.