11.1 C
London
Sunday, July 7, 2024
HomeRDescriptive Statistics in RHow to Use the identical() Function in R (With Examples)

How to Use the identical() Function in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 #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 #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 #check if two vectors are identical
identical(vector1, vector2)

[1] TRUE

The function returns TRUE since the two vectors are indeed exactly identical.

The following code shows how to use the identical() function to test if another two vectors are exactly equal:

#define two vectors
vector1 #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 frame(team=c('A', 'B', 'C', 'D'),
                  points=c(14, 20, 22, 29))

df2 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 frame(team=c('A', 'B', 'C', 'D'),
                  points=c(14, 20, 22, 29))

df2 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.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use the dim() Function in R
How to Use the transform() Function in R
How to Use the intersect() Function in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories