I am trying to build a customized function to detect outliers on a dataset. The output that should return is the same as the function identify_outliers in rstatix()
identify_outliers_custom <- function(dt, values) {
q1 <- quantile(dt)[2]
q3 <- quantile(dt)[4]
iqr <- q3 - q1
lower_bound <- q1 - 1.5* iqr
upper_bound <- q3 + 1.5* iqr
dt$is.outlier <- ifelse(dt > upper_bound | dt < lower_bound, TRUE, FALSE)
dt$is.extreme <- ifelse(dt > q3 + 3 * iqr | dt < q1 - 3 * iqr, TRUE, FALSE)
return(dt)
}
Anyway, by applying this function on a dataset it turns back an error
mtcars %>% identify_outliers_custom(mpg)
Error in xtfrm.data.frame(x) : it is not possible xtfrm the data frame
How could it be corrected?
You are using
dtat wrong places in your custom function. The error can be reproduced by passing a dataframe toquantilefunction.You may try this function.