lapply因异常值poisition

发布于 2025-02-11 10:56:46 字数 343 浏览 0 评论 0原文

我正在使用此代码在数据框架中查找异常值。

out<-lapply(df[,-1], function(x) boxplot.stats(x)$out)

第一列只是名称(这就是为什么我要跳过它)。我找到了这种检测异常值的特定位置的方法。

out_ind <- which(df$col1 %in% c(out))

但是就我而言,对象“ out”不是一个 boxplot.stats(x)$ out ,所以我无法运行 ponter promula 。 如何编写它以在每个数据框架列中找到离群值的位置?

I'm using this code to find the outliers in the data frame.

out<-lapply(df[,-1], function(x) boxplot.stats(x)$out)

The first columns are just the names (that's why I'm skipping it). I've found this method to detect the specific position of the outliers.

out_ind <- which(df$col1 %in% c(out))

But in my case, the object "out" is not a single boxplot.stats(x)$out so I cannot run the which formula.
How can I write it to find the outliers' position in every single data frame column?

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

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

发布评论

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

评论(1

纵性 2025-02-18 10:56:46

您现在有一个列表,因此您需要迭代它,例如使用应用函数:

df  <- mtcars

out<-lapply(df[,-1], function(x) boxplot.stats(x)$out)

out_ind  <- sapply(names(df[,-1]), \(col) which(
    df[[col]] %in% out[[col]]
    )
)
# out_ind
# $cyl
# integer(0)

# $disp
# integer(0)

# $hp
# [1] 31

# $drat
# integer(0)

# $wt
# [1] 16 17

# $qsec
# [1] 9

# $vs
# integer(0)

# $am
# integer(0)

# $gear
# integer(0)

# $carb
# [1] 31

You now have a list, so you need to iterate over it, e.g. using the apply functions:

df  <- mtcars

out<-lapply(df[,-1], function(x) boxplot.stats(x)$out)

out_ind  <- sapply(names(df[,-1]), \(col) which(
    df[[col]] %in% out[[col]]
    )
)
# out_ind
# $cyl
# integer(0)

# $disp
# integer(0)

# $hp
# [1] 31

# $drat
# integer(0)

# $wt
# [1] 16 17

# $qsec
# [1] 9

# $vs
# integer(0)

# $am
# integer(0)

# $gear
# integer(0)

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