【问题标题】:Only include outliers from each column in a dataframe仅包含数据框中每列的异常值
【发布时间】:2015-06-21 02:05:47
【问题描述】:

我有一个如下的数据框:

 chr   leftPos         TBGGT     12_try      324Gtt       AMN2
  1     24352           34         43          19         43
  1     53534           2          1           -1         -9
  2      34            -15         7           -9         -18
  3     3443           -100        -4          4          -9
  3     3445           -100        -1          6          -1
  3     3667            5          -5          9           5
  3     7882           -8          -9          1           3

我必须创建一个循环:

a) 从第三列开始计算每列的上限和下限(UL 和 LL)。
b) 仅包括位于 UL 和 LL (Zoutliers) 之外的行。
c) 然后计算 Zoutlier 与前一个 后一行 相同 chr 方向相同(即正或负)的行数。

因此输出将是:

 ZScore1    TBGGT     12_try      324Gtt       AMN2
 nrow        4         6            4           4

到目前为止,我的代码如下:

  library(data.table)#v1.9.5
  f1 <- function(df, ZCol){

  #A) Determine the UL and LL and then generate the Zoutliers
  UL = median(ZCol, na.rm = TRUE) + alpha*IQR(ZCol, na.rm = TRUE)
  LL = median(ZCol, na.rm = TRUE) - alpha*IQR(ZCol, na.rm = TRUE)
  Zoutliers <- which(ZCol > UL | ZCol < LL)

  #B) Exclude Zoutliers per chr if same direction as previous or subsequent row
  na.omit(as.data.table(df)[, {tmp = sign(eval(as.name(ZCol)))
  .SD[tmp==shift(tmp) | tmp==shift(tmp, type='lead')]},
  by=chr])[, list(.N)]}

  nm1 <- paste0(names(df)
  setnames(do.call(cbind,lapply(nm1, function(x) f1(df, x))), nm1)[]

代码是从不同的地方拼凑在一起的。我遇到的问题是结合代码的 A) 和 B) 部分来获得我想要的输出

【问题讨论】:

  • Zcol 应该本质上是 3:ncol(df) ,即从第 3 列开始的所有列,还是一次只有一列?
  • 它应该一次计算一列。我想代码第一部分的输出应该给我所有的 Z 异常值,其中 chr 和 leftPos 我认为它们在其中。然后,第二部分应采用该列,然后为每个 chr 评估每一行,如所述。这就是想法。那么我应该将 Zoutliers 传递给第二部分吗?
  • 如果我只专注于第一部分 - 我将如何获得与 chr 和 leftPos 相关联的 Zoutliers,然后我可以将其传递给问题的第二部分

标签: r data.table


【解决方案1】:

你可以试试这个功能吗?我不确定alpha 是什么,所以我无法重现预期的输出并将其作为变量包含在函数中。

# read your data per copy&paste
d <- read.table("clipboard",header = T)
# or as in Frank comment mentioned solution via fread
d <- data.table::fread("chr   leftPos         TBGGT     12_try      324Gtt       AMN2
                                     1     24352           34         43          19         43
                                     1     53534           2          1           -1         -9
                                     2      34            -15         7           -9         -18
                                     3     3443           -100        -4          4          -9
                                     3     3445           -100        -1          6          -1
                                     3     3667            5          -5          9           5
                                     3     7882           -8          -9          1           3")


# set up the function
foo <- function(x, alpha, chr){
  # your code for task a) and b)
  UL = median(x, na.rm = TRUE) + alpha*IQR(x, na.rm = TRUE)
  LL = median(x, na.rm = TRUE) - alpha*IQR(x, na.rm = TRUE)
  Zoutliers <- which(x > UL | x < LL)
  # part (c
  # factor which specifies the direction. 0 values are set as positives
  pos_neg <- ifelse(x[Zoutliers] >= 0, "positive", "negative")
  # count the occurrence per chromosome and direction.
  aggregate(x[Zoutliers], list(chr[Zoutliers], pos_neg), length)
}

# apply over the columns and get a list of dataframes with number of outliers per chr and direction.
apply(d[,3:ncol(d)], 2, foo, 0.95, d$chr)

【讨论】:

  • 仅供参考,该软件包现在提供fread 功能,您可以使用该功能阅读DT= fread("text text text") 等文本
  • @Frank 哦,很高兴知道。在我的回答中包含了这个功能。
猜你喜欢
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2017-10-01
  • 2019-12-06
相关资源
最近更新 更多