summarize all variables by group in r

To summarize all variables by group in R, we can use the dplyr package to group the data by one or more variables, and then summarize the data using functions such as sum, mean, median, min, max, etc.

Here is an example of how to summarize the mpg dataset from the ggplot2 package in R, where we group the data by the cyl variable, and calculate summary statistics for all other variables:

main.r
library(dplyr)
library(ggplot2)

# group data by 'cyl' variable and summarize all other variables
mpg_summary <- mpg %>% 
  group_by(cyl) %>% 
  summarise_all(list(mean = mean, sd = sd, min = min, max = max))

# print first few rows of summary data
head(mpg_summary)
267 chars
11 lines

This code will output a summarized dataset that shows the mean, standard deviation, minimum, and maximum values for each variable, grouped by the cyl variable.

gistlibby LogSnag