average of values in an R vector

Average of Values in an R Vector

A vector is a one-dimensional data structure used to store data of the same type in R. Numeric vectors are commonly used to store a sequence of numbers. In this tutorial, we will look at how to get the average of values in an R vector with the help of some examples.

How to get the average of values in a vector in R?

You can use the R mean() function to get the average of values in a vector. Pass the vector as an argument to the function. The following is the syntax –

# average of values in a vector
mean(x, trim=0, na.rm=FALSE)

The following are the arguments that you can give to the mean() function in R.

  • x – The vector for which you want to compute the mean.
  • trim – (Optional argument) Specifies the fraction of values to remove (or trim) from the start and the end of the vector before computing the mean. It is 0 by default.
  • na.rm – (Optional argument) Indicates whether to remove missing values before computing the mean. It is FALSE by default.

The function returns the mean of values in the passed vector.

Examples

Let’s look at some examples of using the above method to get the mean of values in a vector.

Average of values in a numeric vector

Let’s create a vector of numbers (and without any NA values) and apply the mean() function.

# create a vector
vec <- c(1, 2, 3)
# mean of values in vector
mean(vec)

Output:

2

We get the mean as 2, which is the correct mean of the values in the above vector, (1+2+3)/3=2.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

What would happen if there are some NA present values in the vector?

Let’s find out.

First, we will create a vector with some NA values and then apply the mean() function without any additional arguments.

# create a vector with NA values
vec <- c(1, 2, NA, 3, NA)
# mean of values in the vector
mean(vec)

Output:

<NA>

You can see that we get NA as the output. This is because averaging anything with NA results in an NA in R.

Average of values in a vector with NA values

You can pass TRUE to the na.rm parameter of the mean() function to exclude missing values when computing the average in a vector.

# create a vector with NA values
vec <- c(1, 2, NA, 3, NA)
# mean of values in the vector
mean(vec, na.rm=TRUE)

Output:

2

Now we get the average of the values in the above vector as 2.

Trim vector before computing average

With the optional trim parameter in the mean() function, you can specify the fraction of values you want to trim (or exclude) from the start and the end of the vector before computing the mean. Let’s look at an example.

# create a vector
vec <- c(1, 2, 3, 4, 5)
# mean of values in the vector
mean(vec, trim=0.2)

Output:

3

Here, we get the mean of the values in the above vector after trimming the first and last 20% of the values. Since the vector is of length 5, trimming 20% means, we trim one value each from the start and the end. Thus, the average that we get in the output here is the average of the middle three values 2, 3, and 4 which is equal to 3.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top