How to Calculate Average in R

The average of a number represents the middle or central value in the data set. For example, the mode, median, or mean is calculated by dividing the sum of the values set by their number count.

The mathematical formula for calculating the average is the following.

average formula

How to calculate the Average in R

To calculate an Average in R, use the mean() function. 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.

The average is calculated by taking a sum of the input values and dividing it by the number of values in the input data. The mean() function returns the mean of the given numeric vector.

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

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 used the mean() function to calculate the average of the vector, which is 34.57143.

Calculating the average of List in R

To calculate an average of a list in R, use the sapply() and mean() functions. The sapply() function applies a function to all the elements of the list.

To create a list in R, use the list() function. To create a Vector in R, use the c() function or colon operator.

pedro <- 1:4
bella <- 5:8

lastOfUs <- list(pedro, bella, pedro * 2, bella * 3)
lastOfUs

cat("After finding the average of each element of the list", "\n")
sapply(lastOfUs, mean)

Output

[[1]]
[1] 1 2 3 4

[[2]]
[1] 5 6 7 8

[[3]]
[1] 2 4 6 8

[[4]]
[1] 15 18 21 24

After finding the average of each element of the list

[1] 2.5 6.5 5.0 19.5

You can see from the output that the average of the first element is 2.5 because (1 + 2 + 3 +5) / 4 = 2.5.

Same for the second, third, and fourth elements.

To get the mean of the 4th element of the list, use the mean(list[[4]]).

To get the mean of each element of the list, use lapply(list, mean).

Let’s find the average of the fourth element of our lastOfUs list.

pedro <- 1:4
bella <- 5:8

lastOfUs <- list(pedro, bella, pedro * 2, bella * 3)
lastOfUs

cat("After finding the average of fourth element of the list", "\n")
mean(lastOfUs[[4]])

Output

[[1]]
[1] 1 2 3 4

[[2]]
[1] 5 6 7 8

[[3]]
[1] 2 4 6 8

[[4]]
[1] 15 18 21 24

After finding the average of fourth element of the list

[1] 19.5

And we get the average of the fourth element.

Calculating the Average of a data frame in R

To calculate the average of a data frame column in R, use the mean() function. The mean() function takes the column name as an argument and calculates the mean value of that column.

To create a data frame, use the data.frame() function.

df <- data.frame(a1 = 1:3, a2 = 4:6, a3 = 7:9)
df
cat("The average of the a2 column is", "\n")
mean(df$a2)

Output

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

The average of the a2 column is
[1] 5

In this example, the mean() function takes the second column, a2, as an argument because we need to find the average of the a2 column, which is 5. After all, the values of the a2 column are (4, 5, 6), the sum is 15, and the total number is 3. So the average is 15 / 3 = 5.

Conclusion

In this tutorial, we have seen how to calculate the average of a Vector, List, and data frame in R using mean(), lapply(), and sapply() methods. If you have a data set of numerical values,  use the mean() function to calculate an average.

That is it.

See also

Standard deviation in R

Variance in R

Mode in R

Leave a Comment