identical                package:base                R Documentation

_T_e_s_t _O_b_j_e_c_t_s _f_o_r _E_x_a_c_t _E_q_u_a_l_i_t_y

_D_e_s_c_r_i_p_t_i_o_n:

     The safe and reliable way to test two objects for being exactly
     equal.  It returns `TRUE' in this case, `FALSE' in every other
     case.

_U_s_a_g_e:

     identical(x, y)

_A_r_g_u_m_e_n_t_s:

    x, y: Any R objects. 

_D_e_t_a_i_l_s:

     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 wasn't, 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.

     The function `all.equal' is also sometimes used to test equality
     this way, but it was intended for something different.  First, it
     tries to allow for ``reasonable'' differences in numeric results.
     Second, it returns a descriptive character vector instead of
     `FALSE' when the objects do not match.  Therefore, it is not the
     right function to use for reliable testing either.  (If you do
     want to allow for numeric fuzziness in comparing objects, you can
     combine `all.equal' and `identical', as shown in the examples
     below.)

     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.

_V_a_l_u_e:

     A single logical value, `TRUE' or `FALSE', never `NA' and never
     anything other than a single value.

_A_u_t_h_o_r(_s):

     John Chambers

_S_e_e _A_l_s_o:

     `all.equal' for descriptions of how two objects differ; Comparison
     for operators that generate elementwise comparisons.

_E_x_a_m_p_l_e_s:

     identical(1, NULL) ## FALSE -- don't try this with ==
     identical(1, 1.)   ## TRUE in R (both are stored as doubles)
     identical(1, as.integer(1)) ## FALSE, stored as different types

     ## how to test for object equality allowing for numeric fuzz
     identical(all.equal(x, y), TRUE)
     ## If all.equal thinks the objects are different, it returns a
     ## character string, and this expression evaluates to FALSE

