match.arg                package:base                R Documentation

_A_r_g_u_m_e_n_t _V_e_r_i_f_i_c_a_t_i_o_n _U_s_i_n_g _P_a_r_t_i_a_l _M_a_t_c_h_i_n_g

_D_e_s_c_r_i_p_t_i_o_n:

     'match.arg' matches 'arg' against a table of candidate values as
     specified by 'choices', where 'NULL' means to take the first one.

_U_s_a_g_e:

     match.arg(arg, choices, several.ok = FALSE)

_A_r_g_u_m_e_n_t_s:

     arg: a character vector (of length one unless 'several.ok' is
          'TRUE') or 'NULL'.

 choices: a character vector of candidate values

several.ok: logical specifying if 'arg' should be allowed to have more
          than one element.

_D_e_t_a_i_l_s:

     In the one-argument form 'match.arg(arg)', the choices are
     obtained from a default setting for the formal argument 'arg' of
     the function from which 'match.arg' was called.  (Since default
     argument matching will set 'arg' to 'choices', this is allowed as
     an exception to the 'length one unless 'several.ok' is 'TRUE''
     rule, and returns the first element.)

     Matching is done using 'pmatch', so 'arg' may be abbreviated.

_V_a_l_u_e:

     The unabbreviated version of the exact or unique partial match if
     there is one; otherwise, an error is signalled if 'several.ok' is
     false, as per default.  When 'several.ok' is true and more than
     one element of 'arg' has a match, all unabbreviated versions of
     matches are returned.

_S_e_e _A_l_s_o:

     'pmatch', 'match.fun', 'match.call'.

_E_x_a_m_p_l_e_s:

     require(stats)
     ## Extends the example for 'switch'
     center <- function(x, type = c("mean", "median", "trimmed")) {
       type <- match.arg(type)
       switch(type,
              mean = mean(x),
              median = median(x),
              trimmed = mean(x, trim = .1))
     }
     x <- rcauchy(10)
     center(x, "t")       # Works
     center(x, "med")     # Works
     try(center(x, "m"))  # Error
     stopifnot(identical(center(x),       center(x, "mean")),
               identical(center(x, NULL), center(x, "mean")) )

     ## Allowing more than one match:
     match.arg(c("gauss", "rect", "ep"),
               c("gaussian", "epanechnikov", "rectangular", "triangular"),
               several.ok = TRUE)

