To calculate trimmed mean you can use mean() function with trim argument. A trimmed mean is the mean of a dataset that has been calculated after removing a specific percentage of the smallest and largest values from the dataset.

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

Method 1: Use mean() Function

mean(data,trim)

The following example shows how to calculate trimmed mean in R.

## Use mean() To Calculate Trimmed Mean

Let’s see how we can calculate trimmed mean for one of the column of dataframe.

# Create dataframe
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H"),
Pressure1=c(78.2, 28, 71.7, 80.21, 72.7, 30, 84.21, 76.2),
Temperature1=c(31, 33, 36, 37, 36, 33, 37, 31),
Status=c(TRUE,TRUE,FALSE,TRUE,FALSE,TRUE,TRUE,TRUE))

# Calculate trimmed mean
m <- mean(df\$Pressure1,trim = 0.2)

# Display trimmed mean
print(m)

Output:

[1] 68.16833

The output shows trimmed mean for Pressure column of dataframe. Here we calculated 20% trimmed mean value for Pressure1 column of dataframe.

## Use mean() To Calculate Trimmed Mean for Multiple Variables of Data Frame

Let’s apply mean() function to multiple columns of dataframe to calculate trimmed mean.

# Create dataframe
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H"),
Pressure1=c(78.2, 28, 71.7, 80.21, 72.7, 30, 84.21, 76.2),
Temperature1=c(31, 33, 36, 37, 36, 33, 37, 31),
Status=c(TRUE,TRUE,FALSE,TRUE,FALSE,TRUE,TRUE,TRUE))

# Calculate trimmed mean
m <- sapply(df[c("Pressure1","Temperature1")],function(x) mean(x,trim = 0.1))

# Display trimmed mean
print(m)

Output:

Pressure1 Temperature1
65.1525      34.2500

Here the output shows trimmed mean for Pressure1 and Temperature1 column of dataframe.