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

Bobbo picture Bobbo · Nov 12, 2012 · Viewed 27.3k times · Source

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.

Answer

Romain Francois picture Romain Francois · Nov 12, 2012

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 )