0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

outliersパッケージのrm.outlierをもう少し使いやすく

0
Posted at

outliersパッケージのrm.outlier関数の挙動がちょっと気に食わなかったのでググったら、同じことで悩んでいる人がいた。
Error in data.frame with rm.outlier function
何が気に食わないかというと、rm.outlier関数にdataframeを入れた場合、それぞれのcolumnから外れ値を除外したベクトルのリストが返される。このリストに含まれるベクトルの長さが一定ではないために、再びdataframeに戻すことができないため。
なので↓こうした。

outlier.na <- function(df) {
  apply(df, 2, function(x) {
    x[which(x == outlier(x))] <- NA
    x
  })
}
> nrow(iris)
[1] 150
> nrow(na.omit(outlier.na(iris[,-5])))
[1] 144
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?