View source: R/outliers.check.r
outliers.check | R Documentation |
Identifies and optionally replaces outliers in a numeric vector using the Interquartile Range (IQR) method. Outliers are defined as values that fall outside Q1 - k*IQR or Q3 + k*IQR, where k is a user-defined threshold.
outliers.check(raw.vector, sd.thresh = 1.5, fun.replace = "mean", logical = F)
raw.vector |
A numeric vector to check for outliers |
sd.thresh |
Numeric value specifying the threshold multiplier for the IQR (default = 1.5) |
fun.replace |
Character string specifying the replacement method: either "mean" or "median" (default = "mean") |
logical |
Logical value. If TRUE, returns a factor indicating outlier status (1 = outlier, 0 = not outlier). If FALSE, returns the cleaned vector (default = FALSE) |
The function uses the following method to identify outliers: * Calculates Q1 (25th percentile), Q3 (75th percentile), and IQR * Identifies values outside: [Q1 - sd.thresh*IQR, Q3 + sd.thresh*IQR] * Replaces outliers with either mean or median of the original vector
If logical = FALSE (default), returns a numeric vector with outliers replaced by either the mean or median of the original vector. If logical = TRUE, returns a factor vector where 1 indicates outliers and 0 indicates non-outliers.
# Clean outliers using mean replacement
x <- c(1, 2, 3, 100, 2, 3, 4, -50)
outliers.check(x)
# Get logical vector of outlier positions
outliers.check(x, logical = TRUE)
# Clean outliers using median with different threshold
outliers.check(x, sd.thresh = 2, fun.replace = "median")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.