0

I am using the 'across' function to get the summary statistics for a series of variables (for example, all variables that starts with 'f_'. Since the across function will store the summarised results back to the original variable names, having multiple across functions with different summarising functions would overwrite the results (as shown below). I can think of a work-around by renaming the variables after summarise() and cbind the resulting individual tables. However, that seems cumbersome, and I am wondering if there is a tidy (pun intended) way to store the series of summarised results to new variable names.

var_stats = data %>% 
  summarise(
    across(starts_with('f_'), max, na.rm = T),
    across(starts_with('f_'), min, na.rm = T)
  )
PingPong
  • 355
  • 2
  • 11
  • https://stackoverflow.com/questions/46188208/summarise-at-using-different-functions-for-different-variables – jsv Mar 01 '21 at 21:16

1 Answers1

5

With across you can use multiple summary functions at the same time and control the names of the variables. Does the following example help you?

mtcars %>% 
  summarise(across(starts_with("c"), list(custom_min = min, custom_max = max), .names = "{.col}_{.fn}"))

  cyl_custom_min cyl_custom_max carb_custom_min carb_custom_max
1              4              8               1               8
Lennyy
  • 5,932
  • 2
  • 10
  • 23