O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling extreme values with capping

To handle extreme values that lie outside the 1.5 * IQR(Inter Quartile Range) limits, we could cap them by replacing those observations that lie below the lower limit, with the value of 5th percentile and those that lie above the upper limit, with the value of 95th percentile, as shown in the following code:

> replace_outliers <- function(x, removeNA = TRUE) {     pressure_height <- x     qnt <- quantile(pressure_height, probs=c(.25, .75), na.rm = removeNA)     caps <- quantile(pressure_height, probs=c(.05, .95), na.rm = removeNA)     H <- 1.5 * IQR(pressure_height, na.rm = removeNA)     pressure_height[pressure_height < (qnt[1] - H)] <- caps[1]     pressure_height[pressure_height > (qnt[2] + H)] <- caps[2]     pressure_height } ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required