如何使用IQR方法删除/消除异常值

Geo*_*Bar 3 statistics r

我一般都是R和R包的初学者.我想问你下面的问题是否有任何明确的解决办法.我已经以.csv格式导入了我的数据,如下图所示

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

这些是按实体年份分组的数据,大约是4个参数,您可以在下一列中看到.如果还为例如Absrtactions列生成如下框图:

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)
Run Code Online (Sandbox Code Playgroud)

谢谢

bio*_*man 6

你正在寻找合适的功能 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.
Run Code Online (Sandbox Code Playgroud)

所以要删除异常值,你可以做这样的事情

outliersValue<- boxplot.stats(x)$out
x[!x %in% outliersValue]
Run Code Online (Sandbox Code Playgroud)

其中x是您的数据.

%in%运营商将检查值在另一个存在的价值.添加!是一个否定运算符,在这种情况下,它将反转逻辑,返回Truex是未找到的outliersValue

希望这个对你有帮助.快乐的R-ing