.in.                  package:Hmisc                  R Documentation

_F_i_n_d _M_a_t_c_h_i_n_g (_o_r _N_o_n-_M_a_t_c_h_i_n_g) _E_l_e_m_e_n_t_s

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

     For two vectors or scalars 'a' and 'b', 'a %in% b' returns a
     vector of logical values corresponding to the elements in 'a'.  A
     value is 'TRUE' if that element of 'a' is found somewhere in 'b',
     'FALSE' otherwise.  If 'a' is a factor object and 'b' is numeric,
     converts 'a' to its integer codes before the comparison.  If 'a'
     is numeric and 'b' is 'factor', converts 'b' to codes.  '%nin%'
     returns 'TRUE' if the element in 'a' is not in 'b'.

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

     a %in% b
     a %nin% b

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

       a: a vector (numeric, character, factor) 

       b: a vector (numeric, character, factor), matching the mode of a 

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

     vector of logical values with length equal to length of 'a'.

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

     'match'

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

     w <- factor(c("a","b","c"))
     w %in% c("b","c")
     w %in% c(2,3)      #same as previous, but with warning

     #Suppose that a variable x has levels "a", "b1", "b2" and you
     #want to classify levels "b1" and "b2" as just "b".  You can use
     #the %in% operator and do the following:
     # levels(x)[levels(x) %in% c("b1","b2")] <- "b"
     #Note that levels(x) <- list(b=c('b1','b2')) would be a better approach

