2014-11-25 47 views
2

我是一個初學者,通常使用R和R包。 我想問問下面是否有任何明確的解決方案。我已經導入我的.csv格式的數據,你可以在下面的圖片如何使用IQR方法去除/消除異常值

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) 

謝謝

+1

歡迎來到StackOverflow!請閱讀有關如何生成[最小可重現示例]的信息(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610)。這會讓其他人更容易幫助你。 – Jaap 2014-11-25 14:05:42

+0

[在R中輕鬆移除異常值]的可能重複(http://stackoverflow.com/questions/15160485/removing-outliers-easily-in-r) – Jaap 2014-11-25 14:09:16

回答

4

你正在尋找正確的函數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

發現我希望你發現這很有用。 Happy R-ing