How to Use Summarise() to get the Average of Multiple Columns in Dataset

Viewed 35
1 Answers

The mean() will be not get the output as we are passing empty argument i.e

library(dplyr)
mean()

Error in mean.default() : argument "x" is missing, with no default

We need either mean called without anonymous function call (or lambda)

milk_product_facts %>%
    filter(year > 2008) %>% 
    group_by(year) %>%
      summarise(across(everything(), mean))

Or use lambda function

milk_product_facts %>%
   filter(year > 2008) %>%
    group_by(year) %>%
      summarise(across(everything(), ~  mean(.))

Or if we are wrapping with list

milk_product_facts %>%
    filter(year > 2008) %>% 
    group_by(year) %>%
      summarise(across(everything(),  list(~ mean(.))))
Related