• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

R语言函数参数的局部匹配验证(match.arg)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Argument Verification Using Partial Matching

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

  • 用arg匹配choices中指定的参数(限定了输入参数的范围)

Usage

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

Arguments

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.

Details

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.)

  • 如果match.arg函数只有一个参数arg,choices会被自动设置为arg,即choices = arg。此时,在外部函数调用时,每一个输入的参数都会与choices(其实是在外部函数定义时的arg的一个默认参数(长度可以是1,也可以是大于1的向量))
    Matching is done using pmatch, so arg may be abbreviated.
  • 允许输入前几个字母

Value

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.

See Also

pmatch, match.fun, match.call.

Examples

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)

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
R语言绘图边框的单位发布时间:2022-07-18
下一篇:
R语言-变量命名规则发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap