Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR R

group by mean in r

# several summary columns with arbitrary names
library(dplyr)
mtcars %>% 
  group_by(cyl, gear) %>%                            # multiple group columns
  summarise(sum_hp = sum(hp), mean_mpg = mean(mpg))  # multiple summary columns

# summarise all columns except grouping columns using "sum" 
mtcars %>% 
  group_by(cyl) %>% 
  summarise(across(everything(), sum))

# summarise all columns except grouping columns using "sum" and "mean"
mtcars %>% 
  group_by(cyl) %>% 
  summarise(across(everything(), list(mean = mean, sum = sum)))
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #group
ADD COMMENT
Topic
Name
1+7 =