Open In App

How to Calculate a Trimmed Mean in R?

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to calculate trimmed mean in R Programming Language.

A trimmed mean is the mean of the given data that is calculated after removing a specific percentage of the smallest and largest number from the given data.

Example:

Given a set of elements-
[3,4,5,2,8,7,6,9,10,1]
let x = 10% to be trimmed

Solution:
Step 1 : Convert the set into ascending order
 [1 , 2,  3,  4,  5,  6,  7,  8,  9, 10]
 
Step 2 : Remove 10% top and bottom values
 Here 10% means 1 value from top and 1 value from bottom
 so 1 and 10 are removed
 Then the final set is
 [2,  3,  4,  5,  6,  7,  8,  9]
 
Step 3 : Find the mean of the resultant set
[2+3+4+5+6+7+8+9]/8=5.5

To calculate the trimmed mean of the given data, the user has to use the mean() function with the trim parameter.

Syntax:

mean(data,trim)

where, 

  • data is the input data
  • trim is the value percent to be removed

Example:

In this example, we are trimming 10 % of the vector that contains elements from 1 to 10 using the mean() function with the trim argument in the R programming language.

R




# create  a vector
data=c(1:10)
  
# display 
print(data)
  
# calculate trimmed mean with trim of 10%
print(mean(data,trim=0.10))


Output:

[1]  1  2  3  4  5  6  7  8  9 10
[1] 5.5

Example 2:

In this example, we are trimming 10 % of the vector that contains elements from 1 to 20 using the mean() function with the trim argument in the R programming language.

R




# create  a vector
data=c(1:20)
  
# display 
print(data)
  
# calculate trimmed mean with trim of 10%
print(mean(data,trim=0.10))


Output:

[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
[1] 10.5

Example 3:

Under this example, we are trimming the mean of the elements of 5% that contains 5 elements in given columns of the dataframe in the R language.

R




# create dataframe with 3 columns
data=data.frame(col1=c(23,45,32,12,34),
                col2=c(34,56,78,98,76),
                col3=c(45,78,65,32,45))
  
# display dataframe
print(data)
  
# calculate trimmed mean with trim 
# of 5% in col1
print(mean(data$col1,trim=0.05))
  
# calculate trimmed mean with trim 
# of 5% in col2
print(mean(data$col2,trim=0.05))
  
# calculate trimmed mean with trim 
# of 5% in col3
print(mean(data$col3,trim=0.05))


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads