PingPong
PingPong

Reputation: 975

summarizing across multiple variables and assign to new variable names

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)
  )

Upvotes: 1

Views: 928

Answers (1)

Lennyy
Lennyy

Reputation: 6132

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

Upvotes: 6

Related Questions