identical Function in R (Example)
In this tutorial you’ll learn how to test objects for exact equality using the identical function in R.
The page will consist of this:
It’s time to dive into the example:
Constructing Example Data
At first, we need to create some data that we can use in the following examples:
x1 <- letters[1:3] # Create first example vector x1 # Print first example vector # [1] "a" "b" "c"
x2 <- letters[1:3] # Create second example vector x2 # Print second example vector # [1] "a" "b" "c"
x3 <- letters[1:4] # Create third example vector x3 # Print third example vector # [1] "a" "b" "c" "d"
The previous RStudio console outputs show the structure of our example data: We have created three vector objects called x1, x2, and x3.
Let’s compare these vectors!
Example: Apply identical() Function to Vector Objects
In this example, I’ll demonstrate how to apply the identical function to test two data objects for being exactly equal in R.
For this, we have to specify the two data objects that we want to compare within the identical function:
identical(x1, x2) # Apply identical function # [1] TRUE
The previous R code has returned the logical indicator TRUE, i.e. the vector objects x1 and x2 contain exactly the same values.
Let’s compare two other vectors:
identical(x1, x3) # Apply identical function # [1] FALSE
The vectors x1 and x3 are not identical.
Video & Further Resources
Do you need further info on the examples of this article? Then you may want to have a look at the following video on my YouTube channel. I’m explaining the topics of this page in the video:
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.
Furthermore, you could have a look at some of the related tutorials on https://www.statisticsglobe.com/.
- Check If Two Data Frames are the Same
- Compare Vectors and Find Differences
- distinct Function of dplyr Package
- Useful Commands in R (Example Codes)
- All R Programming Examples
In summary: You have learned in this article how to check whether two objects are exactly the same by applying the identical function in the R programming language.
In this tutorial, we have illustrated the usage of the identical function based on character vectors. Note that we could also apply the identical function to other types of data objects such as data frames or lists as well as to different data classes such as numerical or factor values.
Let me know in the comments section below, in case you have any further questions.
Statistics Globe Newsletter