Compare - Exact equality with more flexibility
identical2.RdSimilar to identical() but with more flexibility on how to handle data and
attributes.
identical2()test two objects for exact equality.identical_flag()runsidentical()oridentical2()recursively, returning the comparison results in a list with the same structure asx. Useful to flag wherexandydiffer.
To see if multiple objects are identical, wrap them in a list and use
reduce_predicate(). To vectorize over two lists, checking that each pair of
elements between them are identical, use Map(identical2, x, y) (see
Map()).
Arguments
- x, y
[
any] Any R object.- single_NA, single_zero
[
TRUE|FALSEeach] Whether to treatNAvalues as a identical to each other, and the same for+0vs.-0.- ord_data, ord_attrs
[
TRUE|FALSEeach] Whether to keep the order of the data and attributes inxandy.- tol_type
[
"none"|"abs"|"rel"] Type of tolerance to use when comparing numeric values. One of:"none": no tolerance (default)."abs": absolute tolerance."rel": relative tolerance.
- tol
[
numeric(1)] Tolerance value.- ignore_data
[
character()|integer()] indexes or names of elements to ignore when comparingxandy. Names are compared againstrlang::names2().- ignore_attrs
[
list()] Arguments to pass toattrs_rmv()to remove attributes fromxandy- ...
For
identical_flag(): arguments passed toidentical2()oridentical().- fun
[
"identical"|"identical2"] Foridentical_flag(): function to use for comparison.- .is_attrs
[
TRUE|FALSE] For internal use only.
Value
[
TRUE|FALSE] Foridentical2(): the scalar result of the test.identical_flag()returns a object with the same structure asxand logical elements.
Examples
x <- structure(c(1, 2, 3), a = "a", b = "b", c = "c")
y <- structure(c(3, 2, 1), b = "b", a = "a", c = "d")
identical2(x, y) #> FALSE
#> [1] FALSE
# Pre-sort data and ignore "c" attribute:
identical2(
x, y, ord_data = FALSE,
ignore_attrs = list(exact = c("c"))
) #> TRUE
#> [1] TRUE
# Consider attribute order:
identical2(
x, y, ord_data = FALSE,
ignore_attrs = list(exact = c("c")), ord_attrs = TRUE
) #> FALSE
#> [1] FALSE
# No sorting but accept up to 2.1 numerical absolute tolerance:
identical2(
x, y, tol_type = "abs", tol = 2.1,
ignore_attrs = list(exact = c("c"))
) #> TRUE
#> [1] TRUE
# Understading where the differences are:
identical_flag(x, y)
#> $.data
#> [1] FALSE TRUE FALSE
#>
#> $.attrs
#> $.attrs$a
#> $.attrs$a$.data
#> [1] TRUE
#>
#>
#> $.attrs$b
#> $.attrs$b$.data
#> [1] TRUE
#>
#>
#> $.attrs$c
#> $.attrs$c$.data
#> [1] FALSE
#>
#>
#>
#> $.data
#> c(FALSE, TRUE, FALSE) # First (3 & 1) and third (1 & 3) elements are different
#>
#> $.attrs
#> $.attrs$a
#> $.attrs$a$.data
#> TRUE
#>
#> $.attrs$b
#> $.attrs$b$.data
#> TRUE
#>
#> $.attrs$c
#> $.attrs$c$.data
#> FALSE # Attribute "c" ("c" & "d") is different