How to use duplicated() function In R Language
In this approach, we have used duplicated() to remove all the duplicate rows, here duplicated function is used to check for the duplicate rows, then the column names/variables are passed in the duplicated function.
Note: We have used the NOT(!) operator because we want to filter out or remove the duplicate rows since the duplicated function provides the duplicate rows we negate them using ‘!‘ operator.
Syntax:
df %>%
filter(!duplicated(cbind(col1, col2,..)))
Parameters:
col1,col2: Pass the names of columns based on which you want to remove duplicated values
cbind():It is used to bind together column names such that multiple column names can be used for filtering
duplicated(): returns the duplicate rows
Example: R program to remove duplicate using duplicate()
R
library (dplyr) df <- data.frame (lang = c ( 'Java' , 'C' , 'Python' , 'GO' , 'RUST' , 'Javascript' , 'Cpp' , 'Java' , 'Julia' , 'Typescript' , 'Python' , 'GO' ), value = c (21,21,3,5,180,9,12,21,6,0,3,6), usage = c (21,21,0,99,44,48,53,21,6,8,0,6)) df %>% filter (! duplicated ( cbind (value, usage))) |
Output:
lang value usage 1 Java 21 21 2 Python 3 0 3 GO 5 99 4 RUST 180 44 5 Javascript 9 48 6 Cpp 12 53 7 Julia 6 6 8 Typescript 0 8
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 6