Get difference of dataframes using Dplyr in R
Last Updated :
21 Jul, 2021
In this article, we will discuss How to find the difference between two dataframes using the Dplyr package in the R programming language.
Set difference refers to getting or extracting those values from one dataset that are not present in the other. For this, dplyr supports a function called setdiff(). setdiff() is used to return the data present in the first dataframe but not present in the second dataframe.
Syntax:
setdiff(dataframe1,dataframe2)
Example 1: R program to perform setdiff() operation of the second dataframe with the first dataframe
R
library (dplyr)
data1= data.frame (id= c (1,2,3,4,5),
name= c ( 'sravan' , 'ojaswi' , 'bobby' ,
'gnanesh' , 'rohith' ))
data2= data.frame (id= c (1,2,3,4,5,6,7),
name= c ( 'sravan' , 'ojaswi' , 'bobby' ,
'gnanesh' , 'rohith' ,
'pinkey' , 'dhanush' ))
print ( setdiff (data2,data1))
|
Output:
Example 2: R program to perform setdiff() operation of the first dataframe with the second dataframe
R
library (dplyr)
data1= data.frame (id= c (1,2,3,4,5),
name= c ( 'sravan' , 'ojaswi' , 'bobby' ,
'gnanesh' , 'rohith' ))
data2= data.frame (id= c (1,2,3,4,5,6,7),
name= c ( 'sravan' , 'ojaswi' , 'bobby' ,
'gnanesh' , 'rohith' ,
'pinkey' , 'dhanush' ))
print ( setdiff (data1,data2))
|
Output:
Share your thoughts in the comments
Please Login to comment...