3 Easy Ways to Calculate Average in R

To calculate an Average in R, you can use the mean() function. It takes a numeric vector, list, or data frame column as an argument and returns the sum of its values divided by their number. For example, mean(c(1, 2, 3, 4)) returns 2.5.

The mathematical formula for calculating the average is the following.

average formula

The mean() is a built-in function that accepts numeric vector and boolean value and returns the average of that vector. The Mean is the sum of its data values divided by the data count.

While calculating an average, if the vector contains NA values, they are excluded from the calculation.

Syntax

mean(x, na.rm)

Parameters

x: The x is a Numeric Vector.

na.rm: It is a boolean value to ignore the NA value.

Example 1: Simple program that calculates the average of a vector

rv <- c(11, 21, 19, 18, 51, 51, 71)

# Calculating average using mean() 
mean(rv)

Output

[1] 34.57143

We defined a vector using the c() function and the mean() function to calculate the average of the vector, which is 34.57143.

Alternate approaches

Method 2: Using sum() and length() function to calculate the average

To calculate the average, divide the sum of the vector by the number of elements in the vector. Use the sum() function to calculate the sum of a vector of numbers and the length() function to calculate the number of elements in a vector.

Example

vector <- c(10, 20, 30, 40, 50)
average <- sum(vector) / length(vector)
print(average)

Output

[1] 30

Method 3: Using the aggregate() function to calculate the average

The aggregate() function in R can be used to calculate the average. It accepts an R object like a data frame, grouping variable, and FUN which we will use the mean() function.

Example

df <- data.frame(a1 = 1:3, a2 = 4:6, a3 = 7:9)

aggregate(df, list(df$a2), mean)

Output

   Group.1  a1   a2   a3
1    4       1   4     7
2    5       2   5     8
3    6       3   6     9

FAQ

What is the difference between the mean and average in R

The term “average” refers to a specific mean type, such as the median or mode. In R, the mean() function calculates the arithmetic mean of a set of numbers.

How to calculate the average of a variable in R?

Use the mean() function to calculate the average of a variable in R. Alternate ways are using the sum() along with the length() function and using the aggregate() function.

How to calculate the average of a variable grouped by another variable in R?

Use the aggregate() function to calculate the average of a variable grouped by another variable.

The basic syntax is aggregate(x ~ group, data, mean), where x is the variable, the group is the variable to groupby, and data is either a data frame or matrix.

Conclusion

In this tutorial, we have seen how to calculate the average of a Vector, List, and data frame in R using mean(), sum() along with length(), aggregate(), lapply(), and sapply() methods.

If you have a data set of numerical values,  use the mean() function to calculate an average.

That is it.

Further reading

How to Calculate the Average of List in R

How to Calculate the Average of a data frame in R

Leave a Comment