Avon
Avon

Reputation: 13

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

Upvotes: 0

Views: 1843

Answers (3)

Gigi
Gigi

Reputation: 11

In case you never found out. The brackets at the end were set incorrectly. Here is how it works:

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

Upvotes: 1

Manoj Kumar
Manoj Kumar

Reputation: 5637

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

Upvotes: 1

SPJ
SPJ

Reputation: 116

It would be better with reproducible code.

I would suggest something along the lines of:

data <- iris

output <- data %>%
          group_by(species) %>%
          summarise(across(petal.width:sepal.length),mean)

// or 

output <- data %>%
          group_by(species) %>%
          summarise(across(where(is.numeric), mean))

Upvotes: 0

Related Questions