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.

**Calculate 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.

**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`

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.**

**Calculate Average of List in R**

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

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)
```

**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 first element is 2.5 because (1 + 2 + 3 +5) / 4 = 2.5.

Same for the second, third, and fourth element.

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.

**Calculate 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) and the sum of them 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. That is it for this tutorial.