I have 9 univariate outliers in my dataframe.
I need to make "tuck" those outliers so that they would fit inside 3 standard deviations from the mean, of basically have them assigned the value of the 5th percentile if they're outside the lower limit, and the value of the 95th percentile if they're outside the upper limit.
when you got this error, what was 'yourdata' ? i would guess you passed a dataframe when you should have passed a vector... your function works fine if passed the appropriate arguments.
x <- iris$Petal.Length
caps <- quantile(x, probs=c(.05, .95), na.rm = T)
x[x < caps[1]] <- caps[1]
x[x > caps[2]] <- caps[2]
df <- data.frame(ipl = iris$Petal.Length,
x = x)
df$changemarked <- ifelse(df$x!=df$ipl, "CHANGED","")
fun <- function(x){
quantiles <- quantile( x, c(.05, .95 ) )
x[ x < quantiles[1] ] <- quantiles[1]
x[ x > quantiles[2] ] <- quantiles[2]
x
}
y <- fun( iris$Petal.Length )
identical(x,y)
# same in your function as without...