R – Check if Two Objects are Equal

In R, use identical() when two objects must match exactly, including their type and attributes. Use isTRUE(all.equal()) when you want a single TRUE or FALSE result while allowing the comparison rules and numeric tolerance used by all.equal().

  1. identical() for exact equality
  2. isTRUE(all.equal()) for equality with diagnostic and tolerance-aware comparison

Use identical() to Check Whether Two R Objects Are Exactly Equal

identical(x, y) compares two R objects strictly. It returns TRUE only when the values, data types, dimensions, names, and other relevant attributes match. Otherwise, it returns FALSE.

</>
Copy
> x = c(14, 25, 96, 75)
> y = c(14, 25, 96, 75)
> z = c(58, 5, 526, 75)
> 
> xy = identical(x,y)
> print(xy)
[1] TRUE
> 
> xz = identical(x,z)
> print(xz)
[1] FALSE

Here, x and y contain the same values in the same order, so identical(x, y) returns TRUE. The values in z differ, so identical(x, z) returns FALSE.

Exact Equality Also Includes the Object Type

Two values can look the same when printed but still be different R objects. For example, an integer value and a double value are not identical.

</>
Copy
x <- 1L
y <- 1

identical(x, y)
[1] FALSE

Use identical() in tests, validation checks, and control flow when exact structure and type matter.

Use all.equal() to Compare R Objects with Numeric Tolerance

all.equal(x, y) is designed for practical comparisons, especially for numeric results. It returns TRUE when the objects are considered equal. When they are not equal, it usually returns a character vector describing the difference rather than returning FALSE.

</>
Copy
> x = c(14, 25, 96, 75)
> y = c(14, 54, 96, 75)
> 
> xy = all.equal(x,y)
> print(xy)
[1] "Mean relative difference: 1.16"
> x = c(14, 25, 96, 75)
> y = c(14, 25, 96, 75)
> 
> xy = all.equal(x,y)
> print(xy)
[1] TRUE

Because the unequal result is descriptive text, do not use all.equal() directly as though it always returns a logical value.

Use isTRUE(all.equal()) for a TRUE or FALSE Equality Result

Wrap all.equal() in isTRUE() when your code needs one logical result. isTRUE() returns TRUE only when its argument is exactly the single logical value TRUE.

</>
Copy
> x = c(14, 25, 96, 75)
> y = c(14, 25, 96, 75)
> z = c(58, 5, 526, 75)
> xy = isTRUE(all.equal(x,y))
> print(xy)
[1] TRUE
> xz = isTRUE(all.equal(x,z))
> print(xz)
[1] FALSE

This pattern is useful inside an if statement or a test because the result is always a single logical value.

identical() vs all.equal() in R

Comparisonidentical()all.equal()
Return value when equalTRUETRUE
Return value when differentFALSEA description of the difference
Numeric toleranceStrict comparisonTolerance-aware by default
Type and attributesMust match exactlyCompared according to its arguments
Best useExact identity checksDiagnostics and approximate numeric comparison

Choose identical() when exact identity is required. Choose isTRUE(all.equal()) when comparing computed numeric objects where very small floating-point differences may be acceptable.

Compare Floating-Point Results Safely in R

Floating-point arithmetic can produce tiny rounding differences. Therefore, direct comparison with == may not be suitable for some calculated decimal values.

</>
Copy
x <- 0.1 + 0.2
y <- 0.3

x == y
isTRUE(all.equal(x, y))
[1] FALSE
[1] TRUE

In this case, all.equal() treats the very small numeric difference as acceptable under its default tolerance.

Check Element-by-Element Equality with the == Operator

The == operator serves a different purpose. It compares corresponding elements and returns a logical vector. It does not answer whether two complete objects are exactly identical.

</>
Copy
x <- c(10, 20, 30)
y <- c(10, 25, 30)

x == y
all(x == y)
[1]  TRUE FALSE  TRUE
[1] FALSE

all(x == y) can work for simple vectors, but it may produce NA when missing values are present and it does not perform the same structural checks as identical(). For complete object comparison, prefer identical() or isTRUE(all.equal()).

R Object Equality Examples with NA Values

identical() can compare missing values as part of complete objects without producing an unknown result.

</>
Copy
x <- c(1, NA, 3)
y <- c(1, NA, 3)

identical(x, y)
x == y
[1] TRUE
[1] TRUE   NA TRUE

The element-wise comparison returns NA for NA == NA, while identical() confirms that the complete vectors are identical.

Frequently Asked Questions About Equality in R

What is the difference between == and identical() in R?

== compares elements and returns one logical value per comparison. identical() compares two complete objects and returns one TRUE or FALSE value, with strict checks for type and attributes.

Why does all.equal() return text instead of FALSE?

all.equal() is a diagnostic function. When objects differ, it describes the mismatch so you can understand the reason. Use isTRUE(all.equal(x, y)) when you need a logical result.

How do I compare decimal values in R?

Use isTRUE(all.equal(x, y)) for calculated decimal values because it allows a small numeric tolerance. Use identical() only when exact binary representation and type must match.

Does identical() compare attributes in R?

Yes. Object attributes such as names, dimensions, and classes can affect the result of identical(). Two objects with the same visible values may still return FALSE if their types or attributes differ.

Summary: Choosing an R Equality Function

Use identical() for strict object equality, isTRUE(all.equal()) for tolerance-aware comparison with a logical result, and == for element-by-element comparison. In this R Tutorial, you learned how each method behaves with vectors, numeric values, object types, attributes, and missing values.