all_equal Function of dplyr R Package (2 Examples)
This article explains how to use the all_equal function of the dplyr package in the R programming language.
Table of contents:
- Creating Exemplifying Data
- Example 1: Compare Equal Data Frames with all_equal Function
- Example 2: Compare Unequal Data Frames with all_equal Function
- Video, Further Resources & Summary
Let’s dive right in:
Creating Exemplifying Data
In the examples of this dplyr tutorial, I’ll use the following three data frames:
data1 <- data.frame(x1 = 1:5, # Create three data frames x2 = LETTERS[1:5]) data2 <- data.frame(x1 = 1:5, x2 = LETTERS[1:5]) data3 <- data.frame(x1 = 2:6, x2 = LETTERS[1:5])
Note that the data frames data1 and data2 are exactly the same. The data frame data3 is slightly different.
Furthermore, we need to install and load the dplyr package to RStudio:
install.packages("dplyr") # Install dplyr package library("dplyr") # Load dplyr package
Example 1: Compare Equal Data Frames with all_equal Function
In the first example, we’ll compare the two data frames data1 and data2. We can check whether the two data frames are the same as shown in the following R code:
all_equal(data1, data2) # Compare equal data frames # TRUE
The RStudio console returns the logical value TRUE, indicating that both data frames are exactly the same.
Example 2: Compare Unequal Data Frames with all_equal Function
In the second example, we’ll do a comparison for the two different data frames data1 and data3:
all_equal(data1, data3) # Compare unequal data frames "Rows in x but not y: 1, 2, 3, 4, 5. Rows in y but not x: 1, 2, 3, 4, 5. "
As you can see based on the previous RStudio console output, the all_equal function is not returning the value TRUE anymore. Instead, it returns some explanations at which points our two data frames differ from each other.
Video, Further Resources & Summary
Have a look at the following video that I have published on my YouTube channel. In the video instruction, I explain further functions of the dplyr package in RStudio.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you may want to have a look at some of the other articles of www.statisticsglobe.com. You can find some tutorials about the handling of data frames here:
To summarize: This article showed how to compare data frames with the all_equal command of the dplyr package in R. In case you have any further questions, let me know in the comments below.
Statistics Globe Newsletter