R/remove_outlier.R

Defines functions remove_outliers

Documented in remove_outliers

#' A fucntion to remove outliers in a dataset
#'
#' @param col The main dataset with outliers
#' @param cutoff.value the constant which defines the outliers. It's used in the form of cutoff.value*IQR. Default value is 1.5. 
#' @param na.rm if TRUE, it removes all the NA values. Default value is TRUE
#' @keywords titanfall
#' @export
#' @examples
#' remove_outliers(dataset, cutoff.value = 3)

remove_outliers <- function(col, cutoff.value = 1.5, na.rm = TRUE) {
  qnt <- quantile(col, probs=c(.25, .75), na.rm = TRUE)
  H <- cutoff.value * IQR(col, na.rm = TRUE)
  col.wo.outlier.same.length <- col
  col.wo.outlier.same.length[col < (qnt[1] - H)] <- NA
  col.wo.outlier.same.length[col > (qnt[2] + H)] <- NA
  
  col.wo.outlier.nan <- col.wo.outlier.same.length[!is.na(col.wo.outlier.same.length)]
  
  output <- list(with.nan = col.wo.outlier.same.length, without.nan = col.wo.outlier.nan)
}
1mp3rium/titanfall documentation built on Dec. 16, 2017, 2:44 a.m.