To calculate the average of a value we can use summarise
from the dplyr
package.
library(dplyr)
mtcars %>%
summarise(average_mileage = mean(mpg))
mpg stands for miles per gallon. This gives us the average mileage of all the cars.
If we want to get averages for each cylinder type, then we can use the group_by
function to separate the averages for each group.
mtcars %>%
group_by(cyl) %>%
summarise(average_mileage = mean(mpg))
Top comments (0)