The Wayback Machine - https://web.archive.org/web/20240723070802/https://www.geeksforgeeks.org/how-to-remove-outliers-from-multiple-columns-in-r-dataframe/
Open In App

How to Remove Outliers from Multiple Columns in R DataFrame?

Last Updated : 15 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

In this article, we will discuss how to remove outliers from Multiple Columns in the R Programming Language.

To remove outliers from a data frame, we use the Interquartile range (IQR) method. This method uses the first and third quantile values to determine whether an observation is an outlier to not.

If an observation is 1.5 times the interquartile range greater than the third quartile or 1.5 times the interquartile range less than the first quartile it is considered an outlier. 

Remove Outliers from Multiple Columns in R

To find an outlier in the R Language we use the following function, where we first calculate the first and third quantiles of the observation by using the quantile() function. Then we calculate their difference as an interquartile range.

if an observation is 1.5 times the interquartile range greater than the third quartile or 1.5 times the interquartile range less than the first quartile it returns true.

Syntax:

detect_outlier <- function(x) {

 Quantile1 <- quantile(x, probs=.25)

 Quantile3 <- quantile(x, probs=.75)

 IQR = Quantile3-Quantile1

x > Q3 + (iqr*1.5) | x < Q1 – (iqr*1.5) }

Then once the outlier is identified we remove the outlier by testing them with the above function.

Example 1:

Here, is an example, where we remove outliers from three columns of the data frame.

R




# create sample data frame
sample_data <- data.frame(x=c(10, 8, 120, 14, 11, 90, 13, 15, 200, 25, 5),
                          y=c(400, 35, 50, 704, 80, 55, 900, 75, 60, 500, 10),
                          z=c(10, 300, 20, 90, 800, 70, 5, 850, 75, 20, 30))
print("Display original dataframe")
print(sample_data)
 
# create detect outlier function
detect_outlier <- function(x) {
   
  # calculate first quantile
  Quantile1 <- quantile(x, probs=.25)
   
  # calculate third quantile
  Quantile3 <- quantile(x, probs=.75)
   
  # calculate interquartile range
  IQR = Quantile3 - Quantile1
   
  # return true or false
  x > Quantile3 + (IQR * 1.5) | x < Quantile1 - (IQR * 1.5)
}
 
# create remove outlier function
remove_outlier <- function(dataframe, columns = names(dataframe)) {
   
  # for loop to traverse in columns vector
  for (col in columns) {
     
    # remove observation if it satisfies outlier function
    dataframe <- dataframe[!detect_outlier(dataframe[[col]]), ]
  }
   
  # return dataframe
  print("Remove outliers")
  print(dataframe)
}
 
remove_outlier(sample_data, c('x', 'y', 'z'))


Output:

[1] "Display original dataframe"

x y z
1 10 400 10
2 8 35 300
3 120 50 20
4 14 704 90
5 11 80 800
6 90 55 70
7 13 900 5
8 15 75 850
9 200 60 75
10 25 500 20
11 5 10 30

[1] "Remove outliers"
x y z
1 10 400 10
2 8 35 300
3 120 50 20
4 14 704 90
6 90 55 70
7 13 900 5
10 25 500 20
11 5 10 30

Example 2:

Here, is an example, where we remove outliers from four columns of the data frame.

R




# create sample data frame
sample_data <- data.frame(x=c(-1, 2, 3, 4, 3, 2, 3, 4, 4, 5, 10),
                          y=c(-4, 3, 5, 7, 8, 5, 9, 7, 6, 5, 10),
                          z=c(-1, 3, 2, 9, 8, 7, 0, 8, 7, 2, 13),
                          w=c(10, 0, 1, 0, 1, 0, 1, 0, 2, 2, 10))
print("Display original dataframe")
print(sample_data)
 
# create detect outlier function
detect_outlier <- function(x) {
   
  # calculate first quantile
  Quantile1 <- quantile(x, probs=.25)
   
  # calculate third quantile
  Quantile3 <- quantile(x, probs=.75)
   
  # calculate inter quartile range
  IQR = Quantile3 - Quantile1
   
  # return true or false
  x > Quantile3 + (IQR * 1.5) | x < Quantile1 - (IQR * 1.5)
}
 
# create remove outlier function
remove_outlier <- function(dataframe, columns = names(dataframe)) {
   
  # for loop to traverse in columns vector
  for (col in columns) {
     
    # remove observation if it satisfies outlier function
    dataframe <- dataframe[!detect_outlier(dataframe[[col]]), ]
  }
   
  # return dataframe
  print("Remove outliers")
  print(dataframe)
}
 
remove_outlier(sample_data, c('x', 'y', 'z', 'w'))


Output:

  [1] "Display original dataframe"

x y z w
1 -1 -4 -1 10
2 2 3 3 0
3 3 5 2 1
4 4 7 9 0
5 3 8 8 1
6 2 5 7 0
7 3 9 0 1
8 4 7 8 0
9 4 6 7 2
10 5 5 2 2
11 10 10 13 10

[1] "Remove outliers"
x y z w
2 2 3 3 0
3 3 5 2 1
4 4 7 9 0
5 3 8 8 1
6 2 5 7 0
7 3 9 0 1
8 4 7 8 0
9 4 6 7 2
10 5 5 2 2


Similar Reads

Remove Outliers from Data Set in R
In this article, we will be looking at the approach to remove the Outliers from the data set using the in-built functions in the R programming language. Outliers are data points that don't fit the pattern of the rest of the data set. The best way to detect the outliers in the given data set is to plot the boxplot of the data set and the point locat
2 min read
Ignore Outliers in ggplot2 Boxplot in R
In this article, we will understand how we can ignore or remove outliers in ggplot2 Boxplot in R programming language. Removing/ ignoring outliers is generally not a good idea because highlighting outliers is generally one of the advantages of using box plots. However, sometimes extreme outliers, on the other hand, can alter the size and obscure ot
3 min read
Remove duplicate rows based on multiple columns using Dplyr in R
In this article, we will learn how to remove duplicate rows based on multiple columns using dplyr in R programming language. Dataframe in use: lang value usage 1 Java 21 21 2 C 21 21 3 Python 3 0 4 GO 5 99 5 RUST 180 44 6 Javascript 9 48 7 Cpp 12 53 8 Java 21 21 9 Julia 6 6 10 Typescript 0 8 11 Python 3 0 12 GO 6 6Removing duplicate rows based on t
4 min read
Remove Multiple Columns from data.table in R
In this article, we are going to see how to remove multiple columns from data.table in the R Programming language. Create data.table for demonstration: C/C++ Code # load the data.table package library(&quot;data.table&quot;) # create a data.table with 4 columns # they are id,name,age and address data = data.table(id = c(1,2,3) , name = c(&quot;srav
2 min read
How to plot all the columns of a dataframe in R ?
In this article, we will learn how to plot all columns of the DataFrame in R programming language. Dataset in use: x y1 y2 y3 1 1 0.08475635 0.4543649 0 2 2 0.22646034 0.6492529 1 3 3 0.43255650 0.1537271 0 4 4 0.55806524 0.6492887 3 5 5 0.05975527 0.3832137 1 6 6 0.08475635 0.4543649 0 7 7 0.22646034 0.6492529 1 8 8 0.43255650 0.1537271 0 9 9 0.55
5 min read
How to Combine Two Columns into One in R dataframe?
In this article, we will discuss how to combine two columns into one in dataframe in R Programming Language. Method 1 : Using paste() function This function is used to join the two columns in the dataframe with a separator. Syntax: paste(data$column1, data$column2, sep=" ") where data is the input dataframecolumn1 is the first columncolumn2 to is t
2 min read
Dplyr - Groupby on multiple columns using variable names in R
The group_by() method is used to group the data contained in the data frame based on the columns specified as arguments to the function call. The group_by() function takes as an argument, the across and all of the methods which has to be applied on the specified grouping over all the columns of the data frame. Syntax: group_by(col1, col2,..) This i
2 min read
Add Multiple New Columns to data.table in R
In this article, we will discuss how to Add Multiple New Columns to the data.table in R Programming Language. To do this we will first install the data.table library and then load that library. Syntax: install.packages("data.table") After installing the required packages out next step is to create the table. We create a table with the help of a "da
3 min read
How to Aggregate multiple columns in Data.table in R ?
In this article, we will discuss how to aggregate multiple columns in Data.table in R Programming Language. A data.table contains elements that may be either duplicate or unique. As a result of this, the variables are divided into categories depending on the sets in which they can be segregated. The column values can be summed over such that the co
5 min read
Drop multiple columns using Dplyr package in R
In this article, we will discuss how to drop multiple columns using dplyr package in R programming language. Dataset in use: Drop multiple columns by using the column name We can remove a column with select() method by its column name Syntax: select(dataframe,-c(column_name1,column_name2,.,column_name n) Where, dataframe is the input dataframe and
4 min read
Article Tags :
three90RightbarBannerImg