Dplyr mean emission exclusion

I was wondering if there is a way to calculate the average emission exception using the dplyr package in R? I tried to do something similar, but did not work:

library(dplyr)
w = rep("months", 4)
value = c(1, 10, 12, 9)
df = data.frame(w, value)
output = df %>% group_by(w) %>% summarise(m = mean(value, na.rm = T, outlier = T))

So, in the above example, the output should be 10.333 (average value of 10, 12 and 9) instead of 8 (average value of 1, 10, 12, 9)

Thank!

+1
source share
1 answer

One way would be like this using a package outlier.

library(outlier)
library(dplyr)

df %>%
    group_by(w) %>%
    filter(!value %in% c(outlier(value))) %>%
    summarise(m = mean(value, na.rm = TRUE))

#       w        m
#1 months 10.33333
+4
source

All Articles