Examples for 'base::identical'


Test Objects for Exact Equality

Aliases: identical

Keywords: programming logic iteration

### ** Examples

identical(1, NULL) ## FALSE -- don't try this with ==
[1] FALSE
identical(1, 1.)   ## TRUE in R (both are stored as doubles)
[1] TRUE
identical(1, as.integer(1)) ## FALSE, stored as different types
[1] FALSE
x <- 1.0; y <- 0.99999999999
## how to test for object equality allowing for numeric fuzz :
(E <- all.equal(x, y))
[1] TRUE
identical(TRUE, E)
[1] TRUE
isTRUE(E) # alternative test
[1] TRUE
## If all.equal thinks the objects are different, it returns a
## character string, and the above expression evaluates to FALSE

## even for unusual R objects :
identical(.GlobalEnv, environment())
[1] FALSE
### ------- Pickyness Flags : -----------------------------

## the infamous example:
identical(0., -0.) # TRUE, i.e. not differentiated
[1] TRUE
identical(0., -0., num.eq = FALSE)
[1] FALSE
## similar:
identical(NaN, -NaN) # TRUE
[1] TRUE
identical(NaN, -NaN, single.NA = FALSE) # differ on bit-level
[1] FALSE
### For functions ("closure"s): ----------------------------------------------
###     ~~~~~~~~~
f <- function(x) x
f
<srcref: file "" chars 31:6 to 31:18>
<environment: 0x55ccff519bc0>
g <- compiler::cmpfun(f)
g
<srcref: file "" chars 31:6 to 31:18>
<bytecode: 0x55ccfc5c3c60>
<environment: 0x55ccff519bc0>
identical(f, g)                        # TRUE, as bytecode is ignored by default
[1] TRUE
identical(f, g, ignore.bytecode=FALSE) # FALSE: bytecode differs
[1] FALSE
## GLM families contain several functions, some of which share an environment:
p1 <- poisson() ; p2 <- poisson()
identical(p1, p2)                          # FALSE
[1] FALSE
identical(p1, p2, ignore.environment=TRUE) # TRUE
[1] TRUE
## in interactive use, the 'keep.source' option is typically true:
op <- options(keep.source = TRUE) # and so, these have differing "srcref" :
f1 <- function() {}
f2 <- function() {}
identical(f1,f2)# ignore.srcref= TRUE : TRUE
[1] TRUE
identical(f1,f2,  ignore.srcref=FALSE)# FALSE
[1] FALSE
options(op) # revert to previous state

## Don't show: 
m0 <- m <- structure(cbind(I = 1, a = 1:3), foo = "bar", class = "matrix")
attributes(m0) <- rev(attributes(m))
names(attributes(m0)) # 'dim' remains first, interestingly...
[1] "dim"      "class"    "foo"      "dimnames"
stopifnot(identical(0, -0),     !identical(0, -0, num.eq = FALSE),
          identical(NaN, -NaN), !identical(NaN, -NaN, single.NA = FALSE),
          identical(m, m0),     !identical(m, m0, attrib.as.set = FALSE) )
## End(Don't show)

[Package base version 4.2.3 Index]