79380848

Date: 2025-01-23 11:32:50
Score: 1
Natty:
Report link

same answer as the second part of the one by @r2evans, but for a more recent version of dplyr (1.1.4), without the superseded functions (summarize_at() and one_of)

note that this doesn't throw a warning regarding columns that do not exist in the dataframe, due to the use of any_of()

iris %>%
  select(-Sepal.Length) %>%
  group_by(Species) %>%
  summarize(across(.cols=any_of(c("Sepal.Width",
                                  "Sepal.Length")),
                   .fns=mean))
#  A tibble: 3 × 2
#   Species    Sepal.Width
#   <fct>            <dbl>
# 1 setosa            3.43
# 2 versicolor        2.77
# 3 virginica         2.97
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @r2evans
  • Low reputation (1):
Posted by: Bogdan Micu