A call to identical
is the way to test exact equality in
if
and while
statements, as well as in logical
expressions that use &&
or ||
. In all these
applications you need to be assured of getting a single logical
value. Users often use the comparison operators, such as ==
or
!=
, in these situations. It looks natural, but it is not what
these operators are designed to do in R. They return an object like
the arguments. If you expected x
and y
to be of length
1, but it happened that one of them was not, you will not get a
single FALSE
. Similarly, if one of the arguments is NA
,
the result is also NA
. In either case, the expression
if(x == y)....
won't work as expected.
The function all.equal
is also sometimes used to test equality
this way, but was intended for something different: it allows for
small differences in numeric results.
The computations in identical
are also reliable and usually
fast. There should never be an error. The only known way to kill
identical
is by having an invalid pointer at the C level,
generating a memory fault. It will usually find inequality quickly.
Checking equality for two large, complicated objects can take longer
if the objects are identical or nearly so, but represent completely
independent copies. For most applications, however, the computational cost
should be negligible.
If single.NA
is true, as by default, identical
sees
NaN
as different from NA_real_
, but all
NaN
s are equal (and all NA
of the same type are equal).
Character strings are regarded as identical if they are in different
marked encodings but would agree when translated to UTF-8.
If attrib.as.set
is true, as by default, comparison of
attributes view them as a set (and not a vector, so order is not
tested).
If ignore.bytecode
is true (the default), the compiled
bytecode of a function (see cmpfun
) will be ignored in
the comparison. If it is false, functions will compare equal only if
they are copies of the same compiled object (or both are
uncompiled). To check whether two different compiles are equal, you
should compare the results of disassemble()
.
You almost never want to use identical
on datetimes of class
"POSIXlt"
: not only can different times in the different
time zones represent the same time and time zones have multiple names,
but several of the components are optional.
Note that identical(x, y, FALSE, FALSE, FALSE, FALSE)
pickily
tests for exact equality.