使用 dplyr 平均排除异常值 [英] Mean excluding outliers using dplyr

查看:31
本文介绍了使用 dplyr 平均排除异常值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以使用 R 中的 dplyr 包计算排除异常值的均值?我试图做这样的事情,但没有奏效:

I was wondering if there is a way to compute the mean excluding outliers using the dplyr package in R? I was trying to do something like this, 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))

所以在上面的例子中,输出应该是 10.333(10、12、9 的平均值)而不是 8(1、10、12、9 的平均值)

So in above example, output should be 10.333 (mean of 10, 12, & 9) instead of 8 (mean of 1, 10, 12, 9)

谢谢!

推荐答案

一种方法是使用 outlier 包.

One way would be something like this using the outlier package.

library(outliers) #containing function 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

这篇关于使用 dplyr 平均排除异常值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆