How to find a Trimmed Mean in R

Trimmed Mean in R, A trimmed mean is a dataset’s mean that has been determined after deleting a certain percentage of the dataset’s smallest and greatest values.

In other words, before determining the mean, a tiny percentage of the biggest and smallest values are removed using the Trimmed Mean method of averaging.

A ten percent trimmed mean, for example, represents the mean of a dataset after the ten percent smallest and greatest values have been eliminated.

The following basic syntax is the simplest approach to calculate a trimmed mean in R:

The basis syntax for the calculated 10% trimmed mean

mean(x, trim=0.1)

In reality, the following examples demonstrate how to utilize this function to calculate a trimmed mean.

Calculate the Vector’s Trimmed Mean

The code below demonstrates how to compute a 10% trimmed mean for a vector of data.

Let’s create a data set

data1<-c(10, 8, 12, 10, 9, 7, 25, 23, 11, 9, 18, 19, 15, 11, 12, 14, 23, 19)

Now we can calculate the 10% trimmed mean

mean(data1, trim=0.1)
[1] 13.9375

The 10% trimmed mean is 13.93.

After the last 10% and greatest 10% of values have been eliminated from the dataset, this is the mean of the dataset.

In a data frame, calculate the trimmed mean of each column.

The following code demonstrates how to calculate a 5% trimmed mean in a data frame for a specified column.

data<- data.frame(A=c(10, 8, 12, 10, 9, 7, 25, 23, 11, 9),
                B=c(8, 8, 10, 8, 9, 17, 20, 21, 10, 19),
                C=c(10, 18, 11, 12, 17, 18, 20, 21, 17, 12))
data
       A        B       C
1      10       8       10
2       8       8       18
3      12      10       11
4      10       8       12
5       9       9       17
6       7      17       18
7      25      20       20
8      23      21       21
9      11      10       17
10      9      19       12

Now we can calculate a 7% trimmed mean of points

mean(data$A, trim=0.07)
[1] 12.4

The 7% trimmed mean of the values in the ‘points’ column is 12.4.

After the least and biggest 7% of values have been deleted, this is the mean of the ‘A’ column.

Calculate the Trimmed Mean of a Large Number of Columns

The code below demonstrates how to compute a 5% trimmed mean for numerous columns in a data frame:

data<- data.frame(A=c(10, 8, 12, 10, 9, 7, 25, 23, 11, 9),
                B=c(8, 8, 10, 8, 9, 17, 20, 21, 10, 19),
                C=c(10, 18, 11, 12, 17, 18, 20, 21, 17, 12))

Let’s calculate the 5% trimmed mean of A and B.

sapply(data[c('A', 'B')], function(x) mean(x, trim=0.05))
   A    B
 12.4 13.0

We can observe the following from the output:

The ‘A’ column’s 5 percent trimmed mean is 12.4.

The ‘B’ column has a 5 percent trimmed mean of 13.0.

Intraclass Correlation Coefficient in R-Quick Guide » finnstats

Subscribe to our newsletter!

[newsletter_form type=”minimal”]

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

fourteen + seven =

Ads Blocker Image Powered by Code Help Pro

Quality articles need supporters. Will you be one?

You currently have an Ad Blocker on.

Please support FINNSTATS.COM by disabling these ads blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO