r中对极端离群值的离群处理

问题描述 投票:0回答:1

我正在做一个学校项目,我发现我的一个列中有一些离群值。我相信这些离群值正在损害我的相关性测试。我使用了以下代码来识别这些异常值。

boxplot(df$col)
boxplot(df$col)$out
outliers <- boxplot(df$col)$out

在我识别后,我应该如何处理它们?你能不能提供一些代码,让我可以插进去自动处理它们。我知道有两个极端的异常值。你们会建议只处理这两个,其余的不处理吗?如果是这样,代码会是什么......他们超过2000,是唯一两个超过100的数字。

r statistics outliers
1个回答
1
投票
# Nullify outliers: out_free_df => data.frame 
out_free_df <- within(df, {
    col <- ifelse(col %in% boxplot.stats(col)$out, NA, x)
    }
  )

# Impute outliers with mean: imputed_df => data.frame
imputed_df <- within(out_free_df, {col <- ifelse(is.na(col), mean(col, na.rm = TRUE), col)}
© www.soinside.com 2019 - 2024. All rights reserved.