Table of Contents
The identical() function in R is a useful tool for checking the equality of two objects. It returns a logical value, TRUE or FALSE, indicating whether the two objects are identical or not. This function is particularly helpful when working with large datasets or complex data structures.
To use the identical() function, simply pass in two objects as arguments and it will compare them. If the objects are identical, it will return TRUE, otherwise, it will return FALSE. This function can be used with any type of object in R, including vectors, matrices, lists, and data frames.
Some examples of using the identical() function include checking if two vectors have the exact same elements, comparing two data frames to see if they have the same structure and values, and verifying if two lists contain the same objects in the same order.
In summary, the identical() function in R is a useful tool for quickly and accurately checking the equality of two objects. Its versatility and ease of use make it a valuable function for data analysis and manipulation tasks.
Use the identical() Function in R (With Examples)
The identical() function in R can be used to test whether or not two objects in R are exactly equal.
This function uses the following basic syntax:
identical(x, y, …)
where:
- x: The name of an object in R
- y: The name of another object in R
This function returns TRUE if the two objects are exactly equal or FALSE if they are not.
The following examples show how to use this function to test if two strings, two vectors, and two data frames are exactly equal.
Example 1: Use identical() to Test if Two Strings are Equal
The following code shows how to use the identical() function to test if two strings are equal:
#define two strings string1 <- 'This is some string' string2 <- 'This is some string' #check if two strings are identical identical(string1, string2) [1] TRUE
The function returns TRUE since the two strings are indeed exactly identical.
The following code shows how to use the identical() function to test if another two strings are exactly equal:
#define two strings string1 <- 'This is some string' string2 <- 'This is some cool string' #check if two strings are identical identical(string1, string2) [1] FALSE
The function returns FALSE since the two strings are not exactly identical.
Example 2: Use identical() to Test if Two Vectors are Equal
The following code shows how to use the identical() function to test if two vectors are equal:
#define two vectors vector1 <- c('A', 'B', 'C', 'D', 'E', 'F') vector2 <- c('A', 'B', 'C', 'D', 'E', 'F') #check if two vectors are identical identical(vector1, vector2) [1] TRUE
The following code shows how to use the identical() function to test if another two vectors are exactly equal:
#define two vectors vector1 <- c('A', 'B', 'C', 'D', 'E', 'F') vector2 <- c('A', 'B', 'C', 'D') #check if two vectors are identical identical(vector1, vector2) [1] FALSE
The function returns FALSE since the two vectors are not exactly identical.
Example 3: Use identical() to Test if Two Data Frames are Equal
The following code shows how to use the identical() function to test if two data frames are equal:
#define two data frames df1 <- data.frame(team=c('A', 'B', 'C', 'D'), points=c(14, 20, 22, 29)) df2 <- data.frame(team=c('A', 'B', 'C', 'D'), points=c(14, 20, 22, 29)) #check if two data frames are equal identical(df1, df2) [1] TRUE
The function returns TRUE since the two data frames are indeed exactly identical.
The following code shows how to use the identical() function to test if another two data frames are exactly equal:
#define two data frames df1 <- data.frame(team=c('A', 'B', 'C', 'D'), points=c(14, 20, 22, 29)) df2 <- data.frame(team=c('A', 'B', 'C', 'D'), points=c(99, 20, 22, 29)) #check if two data frames are equal identical(df1, df2) [1] FALSE
The function returns FALSE since the two data frames are not exactly identical.
Cite this article
stats writer (2024). How can I use the identical() function in R and what are some examples of its usage?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-identical-function-in-r-and-what-are-some-examples-of-its-usage/
stats writer. "How can I use the identical() function in R and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-identical-function-in-r-and-what-are-some-examples-of-its-usage/.
stats writer. "How can I use the identical() function in R and what are some examples of its usage?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-identical-function-in-r-and-what-are-some-examples-of-its-usage/.
stats writer (2024) 'How can I use the identical() function in R and what are some examples of its usage?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-identical-function-in-r-and-what-are-some-examples-of-its-usage/.
[1] stats writer, "How can I use the identical() function in R and what are some examples of its usage?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use the identical() function in R and what are some examples of its usage?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
