【问题标题】:How to remove/eliminate outliers using IQR method如何使用 IQR 方法去除/消除异常值
【发布时间】:2015-01-23 13:37:02
【问题描述】:

总的来说,我是 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)

谢谢

【问题讨论】:

标签: r statistics


【解决方案1】:

您正在寻找正确的功能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% 运算符将检查一个值是否存在于另一个值中。添加! 是一个否定运算符,在这种情况下,将反转逻辑,为x 返回True,在outliersValue 中找不到@

我希望你觉得这很有用。快乐的铃声

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多