Does trimmed mean in dplyr give the same result as the regular arithmetic mean?

Hi :slight_smile:

Sorry if this has already been addressed, but I coudn't find the topic anywere.

It looks like if you use the function mean and set the trim argument to a value greater than 0 inside summarise, the output is the same as with the regular arithmetic mean.

If I take the mpg column from the mtcars data.frame and compute the two means separately I obtain this

mean(mtcars$mpg)
[1] 20.09062
mean(mtcars$mpg, trim = 0.3)
[1] 19.17857

Whereas if I do it with summarise it gives this result

mtcars %>%
     summarise(mpg = mean(mpg), mpg_trimmed = mean(mpg, trim = 0.3))

     mpg mpg_trimmed
20.09062    20.09062

Am I doing something wrong? Is there something I'm not getting about how the trim argument works?

Thanks :slight_smile:

Hi @Martin,

The issue with your dplyr code is very simple. mpg in mean(mpg, trim = 0.3) refers to the mpg you created in mpg = mean(mpg). This is why you get the same result. You just need to change the name of your first result:

library(dplyr)

mtcars %>%
  summarize(
    mpg_mean = mean(mpg),
    mpg_trimmed = mean(mpg, trim = 0.3)
  )

  mpg_mean mpg_trimmed
1 20.09062    19.17857
1 Like

Ouch!! Yes, that's why. What a foolish mistake.

Thanks a lot!

You're very welcome :slight_smile:

1 Like

Also consider marking my answer as the solution if it solved your problem.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.