Comparing values of data frames in R Programming – all_equal() Function

all_equal() function in R Language is used to compare the values between data frames.

Syntax:
all_equal(target, current, check.attributes, check.names)

Parameters:
target: object to compare
current: object to be compared with
check.attributes: If attributes of both be compared
check.names: If names be compared

Example 1: Comparing Equal Data frames

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# all_equal function
  
# Create three data frames
data1 <- data.frame(x1 = 1:10,              
                    x2 = LETTERS[1:10])
data2 <- data.frame(x1 = 1:10,
                    x2 = LETTERS[1:10])
data3 <- data.frame(x1 = 2:12,
                    x2 = LETTERS[1:5])
  
# Compare equal data frames
all_equal(data1, data2, check.attributes = FALSE)                     

chevron_right


Output:



TRUE

Here in the above code, we have compared equal data frames data1 and data2, and thus the output seems to be “TRUE”.

Example 2: Comparing Unequal dataframes

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# all_equal function
  
# Create three data frames
data1 <- data.frame(x1 = 1:10,              
                    x2 = LETTERS[1:10])
data2 <- data.frame(x1 = 1:10,
                    x2 = LETTERS[1:10])
data3 <- data.frame(x1 = 2:12,
                    x2 = LETTERS[1:5])
  
# Compare unequal data frames
all_equal(data1, data3, check.names = FALSE)                     

chevron_right


Output:

Rows in x but not y: 1, 2, 3, 4, 5, 6, 8, 9, 10. 
Rows in y but not x: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. 

Here in the above code, we compared the unequal data frames data1 and data3, so we got an error explaining what’s wrong in the code.




My Personal Notes arrow_drop_up

Check this out nerdboinet

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.