How to replace outliers with the 5th and 95th percentile values in R

前端 未结 4 1112
野性不改
野性不改 2020-12-30 11:26

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

4条回答
  •  情书的邮戳
    2020-12-30 12:15

    This would do it.

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

提交回复
热议问题