2

I'm wondering if there's an obvious shorthand for applying a bunch of functions across the whole dataset, that is shorthand for across(everything(), ...). Solution does not have to use dplyr.

Note that

! Using `across()` without supplying `.cols` was deprecated in dplyr 1.1.0.

Example

library(dplyr)
mtcars |> summarize(across(everything(), list(min, max)))

The end goal is something like this use case (but not only):

library(dplyr)
mtcars |> 
  summarize(across(everything(), lst(min, max))) |> 
  pivot_longer(everything(), names_to = c("var", ".value"), names_sep = "_")

# A tibble: 11 × 3
   var     min    max
   <chr> <dbl>  <dbl>
 1 mpg   10.4   33.9 
 2 cyl    4      8   
 3 disp  71.1  472   
 4 hp    52    335   
 5 drat   2.76   4.93
 6 wt     1.51   5.42
 7 qsec  14.5   22.9 
 8 vs     0      1   
 9 am     0      1   
10 gear   3      5   
11 carb   1      8   
> 

(NB: dplyr::lst() gives you correct names.)

Rico
  • 1,998
  • 3
  • 24
  • 46
  • Try `sapply(mtcars, function(i) c(min(i), max(i)))`, see https://stackoverflow.com/questions/30053266/apply-multiple-functions-in-sapply – zx8754 Nov 23 '22 at 20:49

1 Answers1

5

Shorthand for

across(everything(), ...)

is

across(,...)

in dplyr

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Yes I missed the comma -- should we delete the question? Maybe there's sth else that's interesting though. – Rico Nov 23 '22 at 20:32
  • I would suggest to keep the question. It is a interesting question and not obvious! – TarJae Nov 24 '22 at 05:50