Comparing values of data frames in R Programming – all_equal() Function
all_equal() function in R Language is used to compare the values between dataframes.
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
R – compare values of data frames
Example 1: Comparing Equal Data frames
R
# 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 ) |
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
R
# 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 ) |
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.
Contact Us