The mean() function is used to calculate mean value of numeric vector,dataframe or list in R.

The following method shows how you can do it with syntax.

Method: Use mean() Function


The following examples show how to use mean() function in R.

Use mean() Function to Calculate Mean of Vector

Let’s see how we can calculate mean of numeric vector using mean() function:

# Declare vector
x <- c(2.39,11.25,12.15,13.48,13.78,12.89,12.21,12.58)

# Calculate mean of vector
mean_x = mean(x)

# Show mean


[1] 11.34125

As output shows the mean of vector x is 11.34125.

Use mean() Function to Calculate Mean of Data Frame Column

Suppose we want to calculate mean of one the column of data frame using mean() function:

# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F"),
                 Pressure=c(78.2, 71.7, 80.21, 83.12, 82.56, 79.50),
                 Temperature=c(31, 35, 36, 36, 38, 32),

# Find mean value for column of data frame                 
m = mean(df$Pressure)

# Print mean value


[1] 79.215

The mean() function calculate the mean of Pressure column of data frame which is 79.215.