【问题标题】:find rows with outlier values in any columns [duplicate]在任何列中查找具有异常值的行[重复]
【发布时间】:2020-03-04 16:26:14
【问题描述】:

给定如下数据框 df 中的数据,需要提取具有任何列异常值的行。

text = "
A,B,C,D,E,F,G
93,53,221,314,104,721,179
100,58,218,318,93,718,181
601,61,228,829,106,739,190
510,60,229,739,95,707,181
779,51,242,1021,105,756,180
848,57,228,1076,93,710,191
94,52,227,321,95,723,179
712,58,242,954,486,750,180
,53,,10289,298,841,210
696,53,233,929,95,751,180
101,57,220,321,415,796,179
100,60,226,326,104,744,180
181,58,234,415,105,2870,468
,57,,10277,,,918
"
df = read.table(textConnection(text), sep=",", header = T)

异常值在箱线图中定义 - Q1-1.5IQR / Q3+1.5IQR。因此,任何列(一个或多个)具有异常值的行都将在我们的输出集中。

还希望获得第二组行,而不是上面经典定义中的异常值,任何列值仅高于 Q3+1.5IQR 值的行将在我们的输出集中。

要完成这项工作,我面临一些挑战。我想的伪代码如下

  1. 计算每列的箱线图统计数据
  2. 使用 Q1 和 Q3 值获取列值 > Q3 和 的行索引

关于#1,我尝试了以下方法

> sapply(df, boxplot.stats)
      A         B         C         D         E         F         G        
stats Numeric,5 Numeric,5 Numeric,5 Numeric,5 Numeric,5 Numeric,5 Numeric,5
n     12        14        12        14        13        13        14       
conf  Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2 Numeric,2
out   Integer,0 Integer,0 Integer,0 Integer,2 Integer,3 Integer,2 Integer,3

但这并没有给出像 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. 这样可以在 #2 中使用的输出。

【问题讨论】:

  • 到目前为止您尝试过什么?你是否被困在某个特定的地方?
  • 使用您在此处提供的数据非常困难,没有工具(据我所知)本机读取此数据(并返回一个 R 友好对象)。你会考虑做两件事吗? (1) 使用dput(head(x))data.frame(...)(或类似的编程方式)提供样本数据; (2) (比如说)10 行数据可以产生同样的效果吗? (3)A(时间)与这些有什么关系?不向我们提供与您的问题无关的内容也很有帮助。最后,正如 RAB 所建议的,(4) 请展示您到目前为止尝试过的代码,它将有助于指导编码风格等。
  • @r2evans - 更新了问题。
  • 如果你看一次调用的输出,boxplot.stats 正在返回一个list,而返回的值实际上被宣传为 "List with named components as below"。你可能会做类似sapply(df, function(x) boxplot.stats(x)$stats) 的事情来获得更有用的东西。
  • > sapply(df, function(x) boxplot.stats(x)$stats) A B C D E F G [1,] 93.0 51 218.0 314 93 707 179.0 [2,] 100.0 53 223.5 321 95 721 180.0 [3,] 345.5 57 228.0 784 104 744 180.5 [4,] 704.0 58 233.5 1021 106 756 191.0 [5,] 848.0 61 242.0 1076 106 796 191.0 - 我们如何从这里访问upper whisker

标签: r dataframe outliers


【解决方案1】:

我们可以编写一个函数来判断该值是否为异常值

IsOutlier <- function(data) {
   lowerq = quantile(data, na.rm = TRUE)[2]
   upperq = quantile(data, na.rm = TRUE)[4]
   iqr = upperq - lowerq 
   threshold_upper = (iqr * 1.5) + upperq
   threshold_lower = lowerq - (iqr * 1.5)
   data > threshold_upper | data <  threshold_lower 
}

并选择至少有一个异常值的行

df[rowSums(sapply(df, IsOutlier), na.rm = TRUE) > 0, ]

#     A  B   C     D   E    F   G
#8  712 58 242   954 486  750 180
#9   NA 53  NA 10289 298  841 210
#11 101 57 220   321 415  796 179
#13 181 58 234   415 105 2870 468
#14  NA 57  NA 10277  NA   NA 918

同样,第二组我们可以使用这个函数

IsOutlier_upper <- function(data) {
   upperq = quantile(data, na.rm = TRUE)[4]
   lowerq = quantile(data, na.rm = TRUE)[2]
   iqr = upperq - lowerq 
   data > (upperq + 1.5 * iqr) 
}

【讨论】:

    猜你喜欢
    • 2014-05-08
    • 2018-08-01
    • 2012-09-26
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多