To calculate the mean by group in R, we can use the dplyr package from the tidyverse. Specifically, we can use the group_by and summarize functions to group our data by a certain variable and then calculate the mean for each group.
Here is an example using the built-in mtcars dataset:
main.r134 chars7 lines
This will output a table with the average mpg for each number of cylinders in the mtcars dataset:
main.r98 chars7 lines
gistlibby LogSnag