首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用泛型方法只在R中存在异常值时才删除它们

如何使用泛型方法只在R中存在异常值时才删除它们
EN

Stack Overflow用户
提问于 2021-06-15 19:56:30
回答 1查看 23关注 0票数 2

我正在使用一种方法来删除单变量异常值。只有当向量包含异常值时,此方法才能工作。

如何将该方法推广到不带异常值的向量中。我试过用ifelse,但没有成功。

代码语言:javascript
复制
library(tidyverse)

df <- tibble(x = c(1,2,3,4,5,6,7,80))

outliers <- boxplot(df$x, plot=FALSE)$out
print(outliers)
#> [1] 80

# This removes the outliers
df2 <- df[-which(df$x %in% outliers),]

# a new tibble withou outliers
df3 <- tibble(x = c(1,2,3,4,5,6,7,8))

outliers3 <- boxplot(df3$x, plot=FALSE)$out
print(outliers3) # no outliers
#> numeric(0)

# if I try to use the same expression to remove 0 outliers
df4 <- df[-which(df3$x %in% outliers),]

# boxplot gives an error because df4 has 0 observations
# when I was expecting 8 observations
boxplot(df4$x)
#> Warning in min(x): no non-missing arguments to min; returning Inf
#> Warning in max(x): no non-missing arguments to max; returning -Inf
#> Error in plot.window(xlim = xlim, ylim = ylim, log = log, yaxs = pars$yaxs): need finite 'ylim' values
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-15 19:58:33

否定(!),而不是使用-,即使没有异常值也能工作

代码语言:javascript
复制
df3[!(df3$x %in% outliers3),]

-output

代码语言:javascript
复制
# A tibble: 8 x 1
      x
  <dbl>
1     1
2     2
3     3
4     4
5     5
6     6
7     7
8     8

或者如果有异常值,它就移除。

代码语言:javascript
复制
df[!df$x %in% outliers,]
# A tibble: 7 x 1
      x
  <dbl>
1     1
2     2
3     3
4     4
5     5
6     6
7     7
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67992709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档