How to Remove Duplicate Rows in R DataFrame?
Last Updated :
15 Feb, 2022
In this article, we will discuss how to remove duplicate rows in dataframe in R programming language.
Dataset in use:

Method 1: Using distinct()
This method is available in dplyr package which is used to get the unique rows from the dataframe. We can remove rows from the entire which are duplicates and also we cab remove duplicate rows in a particular column.
Syntax:
distinct(dataframe)
distinct(dataframe,column1,column2,.,column n)
Example: R program to remove duplicate rows using distinct() function
R
# load the package
library(dplyr)
# create dataframe
data=data.frame(names=c("manoj","bobby","sravan",
"deepu","manoj","bobby") ,
id=c(1,2,3,4,1,2),
subjects=c("java","python","php",
"html","java","python"))
# remove all duplicate rows
print(distinct(data))
# remove duplicate rows in subjects column
print(distinct(data,subjects))
# remove duplicate rows in namescolumn
print(distinct(data,names))
Output:

Method 2: Using duplicated()
This function will return the duplicates from the dataframe, In order to get the unique rows, we have to specify ! operator  before this method
Syntax:
data[!duplicated(data$column_name), ]
where,
- data is the input dataframe
- column_name is the column where duplicates are removed in this column
Example: R program to remove duplicate rows using duplicated() functionÂ
R
# create dataframe
data=data.frame(names=c("manoj","bobby","sravan",
"deepu","manoj","bobby") ,
id=c(1,2,3,4,1,2),
subjects=c("java","python","php",
"html","java","python"))
# remove duplicate rows in subjects column
print(data[!duplicated(data$subjects), ])
# remove duplicate rows in names column
print(data[!duplicated(data$names), ])
# remove duplicate rows in id column
print(data[!duplicated(data$id), ])
Output:

Method 3 : Using unique()
This will get the unique rows from the dataframe.
Syntax:
unique(dataframe)
To get in a particular columnÂ
Syntax:
unique(dataframe$column_name
Example: R program to remove duplicate rows using unique() function
R
# create dataframe
data=data.frame(names=c("manoj","bobby","sravan",
"deepu","manoj","bobby") ,
id=c(1,2,3,4,1,2),
subjects=c("java","python","php",
"html","java","python"))
# remove duplicate rows in subjects column
print(unique(data$subjects))
# remove duplicate rows in names column
print(unique(data$names))
# remove duplicate rows in id column
print(unique(data$id))
Â
Â
Output:
Â
[1] "java" "python" "php" "html"
[1] "manoj" "bobby" "sravan" "deepu"
[1] 1 2 3 4
Â
Example: R program to apply unique() function in entire dataframe
Â
R
# create dataframe
data=data.frame(names=c("manoj","bobby","sravan",
"deepu","manoj","bobby") ,
id=c(1,2,3,4,1,2),
subjects=c("java","python","php",
"html","java","python"))
# remove duplicate rows in entire dataframe
print(unique(data))
Output:
Similar Reads
How to Remove Rows in R DataFrame? In this article, we will discuss how to remove rows from dataframe in the R programming language. Method 1: Remove Rows by Number By using a particular row index number we can remove the rows. Syntax: data[-c(row_number), ] where. data is the input dataframerow_number is the row index position Exam
2 min read
How to Delete Row(s) in R DataFrame ? In this article, we will see how row(s) can be deleted from a Dataframe in R Programming Language. Deleting a single row For this, the index of the row to be deleted is passed with a minus sign. Syntax: df[-(index), ] Example 1 :Â R # creating a data frame with # some data . df=data.frame(id=c(1,2,3
2 min read
How to remove empty rows from R dataframe? A dataframe can contain empty rows and here with empty rows we don't mean NA, NaN or 0, it literally means empty with absolutely no data. Such rows are obviously wasting space and making data frame unnecessarily large. This article will discuss how can this be done. To remove rows with empty cells w
1 min read
Remove First Row of DataFrame in R In this article, we are going to see how to remove the first row from the dataframe. We can remove first row by indexing the dataframe. Syntax: data[-1,] where -1 is used to remove the first row which is in row position Example 1: R program to create a dataframe with 2 columns and delete the first
1 min read
How to remove a subset from a DataFrame in R ? A subset is a combination of cells that form a smaller data frame formed out from the original data frame. A set of rows and columns can be removed from the original data frame to reduce a part of the data frame. The subset removal can be based on constraints to which rows and columns are subjected
4 min read