- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- How to find mode for an R data frame column?
- How to find the inverse of log10 for an R data frame column?
- How to find the mean of a numerical column by two categorical columns in an R data frame?
- How to find the sum of squared deviations for an R data frame column?
- How to create a new data frame for the mean of rows of some columns from an R data frame?
- How to find the mean of all values in an R data frame?
- How to find the mean of columns of an R data frame or a matrix?
- How to find the mean for multiple columns in an R data frame using mean function not colMeans?
- How to find the column mean of first n number of rows in R data frame?
- How to find the sum of column values of an R data frame?
- How to find the unique values in a column of an R data frame?
- How to find the first quartile for a data frame column in R?
- How to find the row mean for selected columns in R data frame?
- How to extract a single column of an R data frame as a data frame?
- How to find the sum of squared values of an R data frame column?
