Hello, I am new to R and trying to write a function that will remove outliers for many variables in my data frame and create a new variable with the outlier blank. I think the function is working, but the new variable does not appear in the data frame.
remove_outliers <- function (dataset, column, column_new) {
column <- eval(substitute(column), dataset, parent.frame())
Q <- quantile(column, probs = c(.25, .75), na.rm = FALSE)
iqr <- IQR(column)
up <- Q[2] + 1.5 * iqr # Upper Range
low <- Q[1] - 1.5 * iqr # Lower Range
dataset[[column_new]] <- ifelse (column > up, "",
ifelse(column < low, "",
column))
return(dataset)
}
remove_outliers(study,
day35_freecort_cortisone,
"day35_freecort_cortisone_x")