The average of a number represents the middle or central value in the set of data. For example, the mode, median, or mean that calculated by dividing the sum of the values set by their count of the number.
The mathematical formula for calculating the average is the following.
Average in R
To calculate the average in R, use the mean() function. The average is calculated by taking a sum of the input values and dividing by the number of values in the input data. The Mean is the sum of its data values divided by the count of the data.
x: The x is a Numeric Vector.
na.rm: It is a boolean value to ignore the NA value.
rv <- c(11, 21, 19, 18, 51, 51, 71) # Calculating average using mean() mean(rv)
You can see that we defined a vector using the c( ) function and use the mean() function to calculate the average of the vector, which is 34.57143.
Calculating Average of List in R
To create a list in R, use the list() function. To create a Vector in R, use the c() 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)
[]  1 2 3 4 []  5 6 7 8 []  2 4 6 8 []  15 18 21 24 After finding the average of each element of the list  2.5 6.5 5.0 19.5
You can see from the output that the average of 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[]).
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[])
[]  1 2 3 4 []  5 6 7 8 []  2 4 6 8 []  15 18 21 24 After finding the average of fourth element of the list  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)
a1 a2 a3 1 1 4 7 2 2 5 8 3 3 6 9 The average of the a2 column is  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) and the sum is 15, and the total number is 3. So the average is 15 / 3 = 5.
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. That is it for this tutorial.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.