r - 如何使用 IQR 方法移除/消除异常值

标签 r statistics

总的来说,我是 R 和 R 包的初学者。 想问下下面的问题有没有明确的解决方法。我已经以 .csv 格式导入了我的数据,如下图所示

https://dl.dropboxusercontent.com/u/23801982/1234.jpg

这些是按实体年月分组的数据,与您在下一列中看到的 4 个参数有关。如果还为例如生成一个箱线图摘要列如下:

https://dl.dropboxusercontent.com/u/23801982/1234566.jpg

现在我正在尝试识别我使用 boxplot.stats 命令所做的异常值。

但由于分组数据,我不知道如何从结果中排除异常值并将它们导出到新文件(例如 .txt 或 .csv)中。我还看到了一种使用 IQR 进行计算的手动外部方法,但我认为它不适合所需的可导出数据集。

到目前为止我使用的代码是:

rm(list = ls())
library("gdata")

s1 <- read.csv("C:\\Users\\G\\Documents\\R\\Projects\\20141125.csv", header = T)

boxplot(s1$Abstractions ~ s1$Entity, col="green", srt=45) 

boxplot.stats(s1$Abstractions)

谢谢

最佳答案

您正在查看正确的函数 boxplot.stats

看看你可以使用 R 中的哪些函数

?functionName

那么试试

?boxplot.stats

你会看到它在插槽调用中返回异常值

Value:

     List with named components as follows:

   stats: a vector of length 5, containing the extreme of the lower
          whisker, the lower ‘hinge’, the median, the upper ‘hinge’ and
          the extreme of the upper whisker.

       n: the number of non-‘NA’ observations in the sample.

    conf: the lower and upper extremes of the ‘notch’ (‘if(do.conf)’).
          See the details.

     out: the values of any data points which lie beyond the extremes
          of the whiskers (‘if(do.out)’).
     Note that ‘$stats’ and ‘$conf’ are sorted in _in_creasing order,
     unlike S, and that ‘$n’ and ‘$out’ include any ‘+- Inf’ values.

所以要删除异常值,您可以这样做

outliersValue<- boxplot.stats(x)$out
x[!x %in% outliersValue]

其中 x 是您的数据。

%in% 运算符将检查一个值是否存在于另一个值中。添加 ! 是一个否定运算符,在这种情况下,将反转逻辑,为在 outliersValue 中找不到的 x 返回 True

我希望你觉得这很有用。快乐的R-ing

关于r - 如何使用 IQR 方法移除/消除异常值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27128591/

相关文章:

r - 列表中数值向量的累积和

r - slam 软件包安装失败并出现 make 错误

python - Python 中统计模型中的 ADF 测试

使用不同权重选择一组获胜者的算法

python - 如何将 statsmodels 的 VAR 函数拟合的对数差分数据转换回实际值

r - 在 R 中切片数据帧的第二半

用于 pblapply 函数的 R Shiny 进度条

r - 如果 R 包的许可证 X 是,那么该包中的所有内容都必须在 X 下获得许可吗?

当谷歌机器人或其他爬虫发出请求时,asp.net避免在线用户计数增加

algorithm - 3D 最小二乘平面