I found myself having to compute the trimmed mean today in R, and couldn’t immediately find a function that would accomplish this end. What do I mean (ha!) by the trimmed mean?

The trimmed mean removes outliers. We define outliers as any value such that:

Here’s our function:

`#function to compute the trimmed mean`

trimmed_means<-function(data_to_trim){

Outlier_UQ = quantile(data_to_trim, .75)+1.5*IQR(data_to_trim)

Outlier_LQ = quantile(data_to_trim, .25)-1.5*IQR(data_to_trim)

data_trimmed Outlier_LQ]

return(mean(data_trimmed))

}

Simple as that. You can use this in something like ddply to compare with the mean.

Using the JohnsonJohnson dataset on quarterly earnings (dollars) per Johnson & Johnson share 1960–80, we get

`> mean(JohnsonJohnson)`

[1] 4.799762

> trimmed_means(JohnsonJohnson)

[1] 4.523902

The trimmed mean above computing the mean on all observations

### Like this:

Like Loading...