The **mean() function in R** is used to calculate the arithmetic mean value of a data set. If you still don’t know about this function then please check out our instructions below because we will show you more details about it and how to use it in our program.

**What is the mean() function in R?**

The **mean() function in R** calculates the arithmetic mean value of a data set. Mathematically, a mean value is calculated by taking the sum of all the values and dividing it to the number of values in the data:

We can always calculate the mean value in R based on the formula. Here is a list that contains 10 elements, and we can calculate the mean like this:

vt1 <- c(12, 45, 16, 37, 91, 82, 11, 24, 78, 15) mean <- (12+45+16+37+91+82+11+24+78+15)/10 mean

**Output:**

`[1] 41.1`

However, what if our data set has so many values and we cannot calculate it by hand? For example:

vt1 <- c(12, 45, 16, 37, 91, 82, 11, 24, 78, 15) vt2 <- c(23, 24, 75, 45, 21, 89, 25, 12, 11, 10) vt3 <- c(34, 35, 14, 96, 71, 23, 50, 68, 10, 40) x <- c(vt1, vt2, vt3)

Luckily, R has supported us with a built-in function: the** mean() **function. The** mean()** function will automatically calculate the mean value of the data set.

**How to use the mean() function in R**?

**Syntax**:

`mean(x)`

**Parameter**:

**x**: the data set that you want to find the mean value.

**Example:**

vt1 <- c(12, 45, 16, 37, 91, 82, 11, 24, 78, 15) vt2 <- c(23, 24, 75, 45, 21, 89, 25, 12, 11, 10) vt3 <- c(34, 35, 14, 96, 71, 23, 50, 68, 10, 40) x <- c(vt1, vt2, vt3) mean(x)

**Output:**

`[1] 39.56667`

There are also other arguments that we can add into the function, for example:

**na.rm**: If`TRUE`

, NA values will be removed, if`FALSE`

, NA values will be returned.

Example when na.rm is `TRUE`

:

vt1 <- c(12, NA, 16, 37, NA, 82, 11, 24, 78, 15) vt2 <- c(23, 24, NA, 45, 21, 89, NA, 12, 11, 10) vt3 <- c(34, 35, 14, 96, 71, NA, 50, 68, 10, 40) x <- c(vt1, vt2, vt3) mean(x, na.rm=TRUE)

**Output:**

`[1] 37.12`

Example when na.rm is `FALSE:`

vt1 <- c(12, NA, 16, 37, NA, 82, 11, 24, 78, 15) vt2 <- c(23, 24, NA, 45, 21, 89, NA, 12, 11, 10) vt3 <- c(34, 35, 14, 96, 71, NA, 50, 68, 10, 40) x <- c(vt1, vt2, vt3) mean(x, na.rm=FALSE)

**Output:**

`[1] NA`

**Explain**: because there are NA values, which means there are some elements that we cannot know their values. Therefore, we cannot calculate the mean value.

**Summary**

In this tutorial, we have shown you what the** mean() function in R** is and how to use it in our program. The mean() function is a built-in function that helps us find the mean value of our data set.

**Maybe you are interested**:

Hello. My name is Khanh Hai Ngo. I graduated in Information Technology at VinUni. My advanced programming languages include C, C++, Python, Java, JavaScript, TypeScript, and R, which I would like to share with you. You will benefit from my content.

**Name of the university: **VinUni

**Major**: EE

**Programming Languages**: C, C++, Python, Java, JavaScript, TypeScript, R