从r中的多列中删除异常值

发布于 2025-01-25 09:59:53 字数 242 浏览 3 评论 0原文

我使用以下代码来识别不同列上的异常值:

outliers_x1 <- boxplot(mydata$x1, plot=FALSE)$out
outliers_x4 <- boxplot(mydata$x4, plot=FALSE)$out
outliers_x6 <- boxplot(mydata$x6, plot=FALSE)$out

现在,如何通过一个代码从数据集中删除这些异常值?

I used below codes to identify outliers on different columns:

outliers_x1 <- boxplot(mydata$x1, plot=FALSE)$out
outliers_x4 <- boxplot(mydata$x4, plot=FALSE)$out
outliers_x6 <- boxplot(mydata$x6, plot=FALSE)$out

Now, how can I remove those outliers from the dataset by one code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2025-02-01 09:59:53

这将把任何离群值设置为na,然后选择删除所有列包含异常值的所有行。使用任意数量的列。

为方便起见,使用data.table

library(data.table)
library(matrixStats)
##
#   create sample data
#
set.seed(1)
dt <- data.table(x1=rnorm(100), x2=rnorm(100), x3=rnorm(100))
##
#   incorporate possible outliers
#
dt[sample(100, 5), x1:=10*x1]
dt[sample(100, 5), x2:=10*x2]
dt[sample(100, 5), x3:=10*x3]
##
#    you start here...
#    remove all rows where any column contains an outlier
#
indx <- sapply(dt, \(x) !(x %in% boxplot(x, plot=FALSE)$out))
dt[as.logical(rowProds(indx))]

在上面,indx是具有三个逻辑列的矩阵。每个元素都是true,除非相应的列在该行中包含一个异常值。我们使用rowprods(...)matrixstats package toply(&amp;)一起使用3行。不幸的是,这将转换所有数字(1,0),因此我们必须将用作索引的逻辑转换回逻辑> dt 。

##
#   replaces outliers with NA in each column
#
dt.melt <- melt(dt[, id:=seq(.N)], id='id')
dt.melt[, ol:=(value %in% boxplot(value, plot=FALSE)$out), by=.(variable)]
dt.melt[(ol), value:=NA]
result <- dcast(dt.melt, id~variable)[, id:=NULL]
##
#   remove all rows where any column contains an outlier
#
na.omit(result)

在上面的代码中,我们添加了id列,然后熔体(...),因此所有其他列都在一个列中(value),第二列(<代码>变量),指示原始源列。然后,我们将boxplot(...)算法group-wise(通过variable)生成ol列指示异常值。然后,我们将任何value设置为ol == true对应于na。然后,我们使用dcast(...)将您的原始宽格式重新转换为您的原始宽格式,然后删除id

这有点回旋,但是在处理这样的多列时,这种熔体 - 过程 - 播放模式很常见。

最后,na.omit(结果)将删除任何列中具有na的行。如果那是您想要的,则使用第一种方法会更简单。

This will set any outlier values to NA, and then optionally remove all rows where any column contains an outlier. Works with arbitrary number of columns.

Uses data.table for convenience.

library(data.table)
library(matrixStats)
##
#   create sample data
#
set.seed(1)
dt <- data.table(x1=rnorm(100), x2=rnorm(100), x3=rnorm(100))
##
#   incorporate possible outliers
#
dt[sample(100, 5), x1:=10*x1]
dt[sample(100, 5), x2:=10*x2]
dt[sample(100, 5), x3:=10*x3]
##
#    you start here...
#    remove all rows where any column contains an outlier
#
indx <- sapply(dt, \(x) !(x %in% boxplot(x, plot=FALSE)$out))
dt[as.logical(rowProds(indx))]

In the above, indx is a matrix with three logical columns. Each element is TRUE unless the corresponding column contained an outlier in that row. We use rowProds(...) from the matrixStats package to multiply ( & ) the 3 rows together. Unfortunately this converts everything numeric (1, 0), so we have to convert back to logical to use as an index into dt.

##
#   replaces outliers with NA in each column
#
dt.melt <- melt(dt[, id:=seq(.N)], id='id')
dt.melt[, ol:=(value %in% boxplot(value, plot=FALSE)$out), by=.(variable)]
dt.melt[(ol), value:=NA]
result <- dcast(dt.melt, id~variable)[, id:=NULL]
##
#   remove all rows where any column contains an outlier
#
na.omit(result)

In the code above we add an id column, then melt(...) so all other columns are in one column (value) with a second column (variable) indicating the original source column. Then we apply the boxplot(...) algorithm group-wise (by variable) to produce an ol column indicating an outlier. Then we set any value corresponding to ol == TRUE to NA. Then we re-convert to your original wide format with dcast(...) and remove the id.

It's a bit roundabout but this melt - process - dcast pattern is common when processing multiple columns like this.

Finally, na.omit(result) will remove any rows which have NA in any of the columns. If that's what you want it's simpler to use the first approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文