How to find the trimmed mean for a column of an R data frame?


Trimmed mean is the mean that find the mean of values by excluding a small percentage of smallest and largest values. If we have a 5% trimmed mean that means 2.5% of smallest values and 2.5% of largest values are trimmed from the data and then the mean of the remaining data is calculated.

In R, we can simply use trim argument inside mean function to find the trimmed mean. Check out the below Examples to understand how it can be done.

Example 1

Following snippet creates a sample data frame −

x<-rpois(20,10)
df1<-data.frame(x)
df1

The following dataframe is created

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

To find 5% trimmed mean of column x in df1 on the above created data frame, add the following code to the above snippet −

x<-rpois(20,10)
df1<-data.frame(x)
mean(df1$x,trim=0.05)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 9.722222

Example 2

Following snippet creates a sample data frame −

y<-rnorm(20)
df2<-data.frame(y)
df2

The following dataframe is created

y
1 -0.2275112
2 -0.3068841
3 1.4224841
4  -0.2046823
5  -0.6992631
6   1.3496018
7  -1.3079773
8   1.7224761
9   0.4276027
10  0.7502251
11 -0.5527819
12  0.5962074
13 -1.4589839
14  1.8237570
15 -0.4459367
16  0.6482315
17  1.3998148
18 -0.7622238
19  1.1308999
20 -0.2575229

To find 5% trimmed mean of column y in df2 on the above created data frame, add the following code to the above snippet −

y<-rnorm(20)
df2<-data.frame(y)
mean(df2$y,trim=0.05)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 0.2601533

Example 3

Following snippet creates a sample data frame −

z<-rexp(20)
df3<-data.frame(z)
df3

The following dataframe is created

           z
1  3.41614687
2  0.29851851
3  0.72436257
4  0.65434257
5  1.11213684
6  0.08191155
7  2.03424614
8  0.53371331
9  0.08343326
10 0.37775986
11 0.30124390
12 0.42210175
13 0.86609511
14 1.62482612
15 1.30000247
16 4.68351724
17 0.40375151
18 0.36646246
19 0.92898766
20 0.47307554

To find 5% trimmed mean of column z in df3 on the above created data frame, add the following code to the above snippet −

z<-rexp(20)
df3<-data.frame(z)
mean(df3$z,trim=0.05)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 0.8845115

Updated on: 02-Nov-2021

346 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements