# 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)

## Output

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

 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 −

 0.8845115