R Advanced

Calculating the Mean by Group of a Data Frame in R

Here are three ways to calculate the mean by group for single or multiple columns in the R data frame:

  1. Using base R’s aggregate()
  2. Using dplyr’s group_by() and summarize()
  3. Using data.table package

What does it mean when we say “mean by group”? It means grouping the data based on the values of single or multiple columns and then calculating the mean (average) of those values, but doing so separately for each group.

Here is the sample data frame:

Method 1: Using base R’s aggregate()

The aggregate() function splits the data into subgroups and calculates the summary for each group. In the context of our article, we can split the data based on the specific column(s) groups and calculate the mean for each group.

Mean of a single column grouped by a single column

In our df_sales_data dataset, we can group the data frame by the Category column and calculate the mean of only one column (Price) for each Category.

The aggregate() function accepts three arguments:

  1. Price ~ Category: The Price column values should be grouped by Category.
  2. df_sales_data: It is an input data frame
  3. FUN: We want to calculate the mean of Price grouped by Category.
df_sales_data <- data.frame(
  Product = c("Apple", "Banana", "Apple", "Milk", "Bread", "Butter", "Milk"),
  Category = c("Fruit", "Fruit", "Fruit", "Dairy", "Bakery", "Dairy", "Dairy"),
  Price = c(1.2, 0.5, 1.2, 2.5, 1.8, 2.0, 2.5),
  Quantity = c(5, 10, 5, 2, 3, 12, 2),
  stringsAsFactors = FALSE
)

print(df_sales_data)

# Mean Price by Category
aggregate(Price ~ Category, data = df_sales_data, FUN = mean)

Output

In our data frame,

  1. The bakery category has only one product, Bread, which has a price of 1.8, so the mean price is 1.8000000.
  2. The Dairy category has three products: Milk, Butter, and Milk, and their prices are 2.5, 2, and 2.5. The total price is 7, and if you divide 7 by 3, the mean is 2.333333.
  3. The Fruit category has three products: Apple, Banana, and Apple. Their respective prices are 1.2, 0.5, and 1.2. The mean of these prices is 0.9666667.

Mean of multiple columns grouped by a single column

You can calculate the mean of multiple columns (e.g., Price and Quantity) grouped by a single column (Category) using the aggregate() function.

df_sales_data <- data.frame(
  Product = c("Apple", "Banana", "Apple", "Milk", "Bread", "Butter", "Milk"),
  Category = c("Fruit", "Fruit", "Fruit", "Dairy", "Bakery", "Dairy", "Dairy"),
  Price = c(1.2, 0.5, 1.2, 2.5, 1.8, 2.0, 2.5),
  Quantity = c(5, 10, 5, 2, 3, 12, 2),
  stringsAsFactors = FALSE
)

print(df_sales_data)


# Mean Price and Quantity by Category
aggregate(cbind(Price, Quantity) ~ Category, data = df_sales_data, FUN = mean)

Output

Mean of multiple columns grouped by multiple variables

As the name suggests, we can calculate the mean of multiple columns (Price and Quantity) grouped by multiple categorical variables (Category and Product).

df_sales_data <- data.frame(
  Product = c("Apple", "Banana", "Apple", "Milk", "Bread", "Butter", "Milk"),
  Category = c("Fruit", "Fruit", "Fruit", "Dairy", "Bakery", "Dairy", "Dairy"),
  Price = c(1.2, 0.5, 1.2, 2.5, 1.8, 2.0, 2.5),
  Quantity = c(5, 10, 5, 2, 3, 12, 2),
  stringsAsFactors = FALSE
)

print(df_sales_data)

# Mean Price and Quantity group by Category and Product
aggregate(cbind(Price, Quantity) ~ Category + Product, data = df_sales_data, FUN = mean)

Output

Method 2: Using dplyr’s group_by() and summarise()

If you combine the group_by() and summarise() functions, the output will be in a tibble.

The group_by() method accepts a data frame and one or more columns as arguments, grouping the data frame based on the unique values of the provided columns.

Then, we use the summarise() function, which accepts a function—mean in our case—to calculate the mean for each group.

Before using dplyr, you must install it in your environment and then load it using the code below:

library(dplyr)

Mean of a Single Column Grouped by a Single Column

Let’s calculate the mean of Price grouped by Category:

library(dplyr)


df_sales_data <- data.frame(
  Product = c("Apple", "Banana", "Apple", "Milk", "Bread", "Butter", "Milk"),
  Category = c("Fruit", "Fruit", "Fruit", "Dairy", "Bakery", "Dairy", "Dairy"),
  Price = c(1.2, 0.5, 1.2, 2.5, 1.8, 2.0, 2.5),
  Quantity = c(5, 10, 5, 2, 3, 12, 2),
  stringsAsFactors = FALSE
)

print(df_sales_data)

# Calculate the mean of Price grouped by Category:
df_sales_data %>%
  group_by(Category) %>%
  summarise(Mean_Price = mean(Price, na.rm = TRUE))

Output

Mean of multiple columns grouped by a single column

Let’s find the mean of Price and Quantity, grouped by Category.

library(dplyr)


df_sales_data <- data.frame(
  Product = c("Apple", "Banana", "Apple", "Milk", "Bread", "Butter", "Milk"),
  Category = c("Fruit", "Fruit", "Fruit", "Dairy", "Bakery", "Dairy", "Dairy"),
  Price = c(1.2, 0.5, 1.2, 2.5, 1.8, 2.0, 2.5),
  Quantity = c(5, 10, 5, 2, 3, 12, 2),
  stringsAsFactors = FALSE
)

print(df_sales_data)

# Calculate the mean of Price and Quantity grouped by Category

df_sales_data %>%
  group_by(Category) %>%
  summarise(
    Mean_Price = mean(Price, na.rm = TRUE),
    Mean_Quantity = mean(Quantity, na.rm = TRUE)
  )

Output

Mean of Multiple Columns Grouped by Multiple Variables

Let’s calculate the mean of Price and Quantity grouped by Category and Product:

library(dplyr)


df_sales_data <- data.frame(
  Product = c("Apple", "Banana", "Apple", "Milk", "Bread", "Butter", "Milk"),
  Category = c("Fruit", "Fruit", "Fruit", "Dairy", "Bakery", "Dairy", "Dairy"),
  Price = c(1.2, 0.5, 1.2, 2.5, 1.8, 2.0, 2.5),
  Quantity = c(5, 10, 5, 2, 3, 12, 2),
  stringsAsFactors = FALSE
)

print(df_sales_data)

# Calculate the mean of Price and Quantity grouped by Category and Product
df_sales_data %>%
   group_by(Category, Product) %>%
   summarise(
   Mean_Price = mean(Price, na.rm = TRUE),
   Mean_Quantity = mean(Quantity, na.rm = TRUE)
 )

Output

 

Weighted mean

A weighted mean (also known as a weighted average) is a mean where some values contribute more significantly than others to the final result.

library(dplyr)


df_sales_data <- data.frame(
  Product = c("Apple", "Banana", "Apple", "Milk", "Bread", "Butter", "Milk"),
  Category = c("Fruit", "Fruit", "Fruit", "Dairy", "Bakery", "Dairy", "Dairy"),
  Price = c(1.2, 0.5, 1.2, 2.5, 1.8, 2.0, 2.5),
  Quantity = c(5, 10, 5, 2, 3, 12, 2),
  stringsAsFactors = FALSE
)

print(df_sales_data)

# Calculate weighted mean
df_sales_data %>%
  group_by(Category) %>%
  summarize(weighted_price = weighted.mean(Price, Quantity))

Output

Method 3: Using data.table

Install the data.table package if you have not installed it already:

Let’s find the basic grouped mean of Price and Quantity by Category:

library(dplyr)


df_sales_data <- data.frame(
  Product = c("Apple", "Banana", "Apple", "Milk", "Bread", "Butter", "Milk"),
  Category = c("Fruit", "Fruit", "Fruit", "Dairy", "Bakery", "Dairy", "Dairy"),
  Price = c(1.2, 0.5, 1.2, 2.5, 1.8, 2.0, 2.5),
  Quantity = c(5, 10, 5, 2, 3, 12, 2),
  stringsAsFactors = FALSE
)

print(df_sales_data)

# Basic Grouped Mean using data.table
dt <- as.data.table(df_sales_data)
dt[, .(mean_price = mean(Price), mean_quantity = mean(Quantity)), by = Category]

Output

That’s all!

Recent Posts

file.rename(): Renaming Single and Multiple Files in R

To rename a file in R, you can use the file.rename() function. It renames a…

15 hours ago

R prop.table() Function

The prop.table() function in R calculates the proportion or relative frequency of values in a…

20 hours ago

exp() Function: Calculate Exponential of a Number in R

The exp() is a built-in function that calculates the exponential of its input, raising Euler's…

21 hours ago

R split() Function: Splitting a Data

The split() function divides the input data into groups based on some criteria, typically specified…

1 week ago

colMeans(): Calculating the Mean of Columns in R Data Frame

The colMeans() function in R calculates the arithmetic mean of columns in a numeric matrix,…

2 weeks ago

rowMeans(): Calculating the Mean of rows of a Data Frame in R

The rowMeans() is a built-in, highly vectorized function in R that computes the arithmetic mean…

3 weeks ago