Search code examples
rdplyrsummarize

What is the correct way to calculate Mean across all variables by grouping


A <- data %>% 
      group_by(Agent) %>%
      summarise(across(EP:Yt.ha),mean)

The Error message is

Error: Problem with summarise() input ..2. x Input ..2 must be a vector, not a function. i Input ..2 is mean. i The error occured in group 1: Agent = "Hydro". Run rlang::last_error() to see where the error occurred.

I remember doing it by this way longback but I believe that now I must have misplaced some functions or code. Can someone tell the correct way? There are 13 variable across EP to Yt.ha


Solution

  • try something like this ( you can add group by conditions):

    # install if not already
    install.packages("tidyverse", dependencies = T)
    
    # load package
    library(tidyverse)
    
    # load data
    data(iris)
    iris
    
    # find the mean, grouped by species
    iris %>% group_by(Species) %>% summarise(across(Petal.Width:Sepal.Length, ~ mean(.x, na.rm = TRUE)))
    
    # alternate way to find the mean for specific cols (using col index numbers)
    iris %>% group_by(Species) %>% summarise(across(c(1, 3:4), ~ mean(.x, na.rm = TRUE)))