2024-11-18

Learn R - Compute group means

Here are six boxes filled with either potatos or apples. 

Dataframe df has a group column that indicates whether it's a potato or apple box and a weight colmn that contains the weight of the box.

> df <- data.frame(
+   group=c('potato', 'potato', 'apple', 'apple', 'potato', 'apple'),
+   weight=c(10, 2, 5, 26, 3, 6))

It looks like this.

> df
   group weight
1 potato     10
2 potato      2
3  apple      5
4  apple     26
5 potato      3
6  apple      6

What can we do to get the average weights for the potato and apple boxes, respectively?

First, let's load dplyr.

> library(dplyr)

Then, with df,

  1. group by them with apple/potato group column
  2. send the output via piping (%>%)
  3. summarize the mean of weight and save them with a new column mean.weight.

 df %>% group_by(group) %>% summarize(mean.weight = mean(weight))

# A tibble: 2 × 2
  group  mean.weight
  <chr>        <dbl>
1 apple         12.3
2 potato         5  

The avarage weight for apple boxes is 12.3, and the average weight for potato boxes is 5.

It's that simple! Enjoy coding in R!