如何用R中的第5个和第95个百分位数替换离群值 [英] How to replace outliers with the 5th and 95th percentile values in R

查看:282
本文介绍了如何用R中的第5个和第95个百分位数替换离群值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将相对较大的 R 数据集中的所有值替换为分别位于第95个百分点和第5个百分点以下的值。我的目标是避免只从数据中完全剔除这些离群值。

I'd like to replace all values in my relatively large R dataset which take values above the 95th and below the 5th percentile, with those percentile values respectively. My aim is to avoid simply cropping these outliers from the data entirely.

任何建议都将不胜感激,我在其他任何地方都找不到有关如何执行此操作的信息。 。

Any advice would be much appreciated, I can't find any information on how to do this anywhere else.

推荐答案

这可以做到。

fun <- function(x){
    quantiles <- quantile( x, c(.05, .95 ) )
    x[ x < quantiles[1] ] <- quantiles[1]
    x[ x > quantiles[2] ] <- quantiles[2]
    x
}
fun( yourdata )

这篇关于如何用R中的第5个和第95个百分位数替换离群值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆