You are currently viewing Calculate Mean or Average in R

The mean() is a generic function in R that is used to calculate the arithmetic mean or average of a Vector or DataFrame column. This function accepts a vector as input and returns the average as a numeric value.

Advertisements

In simple words mean or average is calculated by adding up all the values and dividing the sum by the number of values. For example, the mean of the numbers 6, 7, 8 is 7 since 6 + 7 + 8 = 21 and 21 divided by 3 results in 7.

1. Syntax of mean()

Following is the syntax of the mean() function.


# Syntax
mean(x, …)

# S3 method for default
mean(x, trim = 0, na.rm = FALSE, …)
  • x – It is an input vector of type Numeric.
  • trim – Used to drop some observations from both ends of the sorted vector.
  • na.rm – Defaults to FALSE. When TRUE, it ignores NA value.

2. R Mean of DataFrame Column

mean() in R is a base function that is used to calculate the arithmetic mean of a numeric Vector. Since every DataFrame column is a Vector, let’s calculate the mean of a DataFrame column.


# Create Data Frame
df <- data.frame(id=c(11,22,33,44,55),
              price=c(144,NA,321,567,567))
df

# Calculate mean of DataFrame column
res <- mean(df$id)
res

Yields below output.

mean in r

Calculating the mean on a column that has NA values results in NA, you need to ignore the NA to get the right result. Let’s calculate the mean on the column that has NA values by using the na.rm param to ignore NA values. On our DataFrame, we have a column price that has NA values.


# Calculate mean of DataFrame column
# With out ignoring NA
mean(df$price)

# Output
# [1] NA

# Calculate mean of DataFrame column
# Ignoring NA
mean(df$price, na.rm=TRUE)

# Output
# [1] 399.75

3. Mean of R Vector

Similarly, let’s calculate the mean or average of a Vector. The following example demonstrates calculating mean of Vector with out NA and with NA values.


# Calculate mean of Vector
vec = c(6,7,8)
mean(vec)

# Output
# [1] 7

# Calculate mean of Vector
vec = c(6,7,8, NA)
mean(vec, na.rm=TRUE)

# Output
# [1] 7

Conclusion

In this article, you have learned how o calculate the mean or average in R with examples of DataFrame and Vector. Also learned using na.rm=TRUE param to ignore the NA values.

Related Articles

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium