A trimmed mean is a mean trimmed by x%, where x is the percentage of observations removed from both the upper and lower bounds.
For instance, 10% trimmed mean is the mean computed by excluding th 10% largest and 10% smallest observations in the data.
mean()
in R has the parameter of trim
, which should be in the range of (0 to 0.5). The default value is 0.
mean(x, trim = 0, na.rm = FALSE)
Example 1: Trimmed mean of a vector
x <- c(4,5,8,44,32,22,45,3,8,99,100,120)
mean(x, trim=0.1)
Output:
> x <- c(4,5,8,44,32,22,45,3,8,99,100,120) > mean(x, trim=0.1) [1] 36.7
We can see that it trimmed 10% of the vector, and the trimmed mean is 36.7.
Example 2: Trimmed mean for the whole dataframe
# create a dataframe
df<-data.frame(X=c(4,8,8,44,32,22,45,3,12),
Y=c(5,5,7,34,33,80,120,4,8))
# 2% trimmed means of X and Y
sapply(df, function(x) mean(x, trim=0.02))
Output:
> sapply(df, function(x) mean(x, trim=0.02)) X Y 19.77778 32.88889
Example 3: Trimmed mean for a column within a dataframe
The following shows two methods to calculate trimmed means for a column within a dataframe. We can see both methods have the same output, namely 19.78.
# create a dataframe
df<-data.frame(X=c(4,8,8,44,32,22,45,3,12),
Y=c(5,5,7,34,33,80,120,4,8))
# Method 1: 2% trimmed means of X
mean(df$X, trim=0.02)
# Method 2: 2% trimmed means of X
sapply(df['X'], function(x) mean(x, trim=0.02))
Output:
> # Method 1: 2% trimmed means of X > mean(df$X, trim=0.02) [1] 19.77778 > > # Method 2: 2% trimmed means of X > sapply(df['X'], function(x) mean(x, trim=0.02)) X 19.77778