找回密码
 注册
查看: 7535|回复: 0

R语言:switch()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-2-16 20:46:36 | 显示全部楼层 |阅读模式
switch(base)
switch()所属R语言包:base

                                        Select One of a List of Alternatives
                                         选择的替代品列表

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

switch evaluates EXPR and accordingly chooses one of the further arguments (in ...).
switch评估EXPR并据此选择了进一步的论据之一(...)。


用法----------Usage----------


switch(EXPR, ...)



参数----------Arguments----------

参数:EXPR
an expression evaluating to a number or a character string.
一个数字或字符串表达式。


参数:...
the list of alternatives.  If it is intended that EXPR has a character-string value these will be named, perhaps except for one alternative to be used as a "default" value.
替代品列表。如果打算EXPR有一个字符的字符串值将被命名为,除了一个选择也许是要作为一个“默认”值。


Details

详情----------Details----------

switch works in two distinct ways depending whether the first argument evaluates to a character string or a number.
switch工作在两种不同的方式取决于是否第一个参数计算为一个字符串或数字。

If the value of EXPR is not a character string it is coerced to integer.  If this is between 1 and nargs()-1 then the corresponding element of ... is evaluated and the result returned: thus if the first argument is 3 then the fourth argument is evaluated and returned.
如果EXPR值不是一个字符串,它被强制转换为整数。如果这是介于1和nargs()-1然后相应的元素...评估和返回的结果,因此,如果第一个参数是3然后在第四个参数进行评估并返回。

If EXPR evaluates to a character string then that string is matched (exactly)to the names of the elements in ....  If there is a match then that element is evaluated unless it is missing, in which case the next non-missing element is evaluated, so for example switch("cc", a=1, cc=, cd=, d=2) evaluates to 2. If there is more than one match, the first matching element is used. In the case of no match, if there is a unnamed element of ... its value is returned.  (If there is more than one such argument  an error is returned.  Before R 2.13.0 the first one would have been used.)
EXPR如果计算字符串,那么该字符串匹配(精确)在...元素的名称。如果有一场比赛,然后该元素进行评估,除非它被丢失,在这种情况下,未来的不可缺少的元素是评估,例如switch("cc", a=1, cc=, cd=, d=2)值2。如果有一个以上的比赛,第一个匹配的元素。在没有比赛的情况下,如果有一个...无名元素返回它的值。 (如果有多个这样的说法错误,则返回。ř2.13.0之前,第一个会被使用。)

The first argument is always taken to be EXPR: if it is named its name must (partially) match.
采取的第一个参数始终是EXPR:如果它被命名为它的名字(部分)必须匹配。

This is implemented as a primitive function that only evaluates its first argument and one other if one is selected.
这是一个原始的功能,只计算其第一个参数和另外一个,如果一个被选中实施。


值----------Value----------

The value of one of the elements of ..., or NULL, invisibly (whenever no element is selected).
值...或NULL,无形的(只要没有元素被选中)的元素之一。

The result has the visibility (see invisible) of the element evaluated.
结果有能见度(见invisible)元素的评估。


警告----------Warning----------

Before R 2.11.0 it was necessary to avoid partial matching: an alternative E = foo matched the first argument EXPR unless that was named.
2.11.0前ŕ,它是必要的,以避免部分匹配:替代E = foo匹配EXPR除非被命名为第一个参数。

It is possible to write calls to switch that can be confusing and may not work in the same way in earlier versions of R.  For compatibility (and clarity), always have EXPR as the first argument, naming it if partial matching is a possibility.  For the character-string form, have a single unnamed argument as the default after the named values.
这是可能的写调用switch可以是混乱,不得以同样的方式工作,在早期版本的R的兼容性和清晰度,总是有EXPR作为第一个参数,命名如果部分匹配是一种可能性。对于字符的字符串形式,有一个无名参数作为默认后命名值。


参考文献----------References----------

The New S Language. Wadsworth & Brooks/Cole.

举例----------Examples----------


require(stats)
centre <- function(x, type) {
  switch(type,
         mean = mean(x),
         median = median(x),
         trimmed = mean(x, trim = .1))
}
x <- rcauchy(10)
centre(x, "mean")
centre(x, "median")
centre(x, "trimmed")

ccc <- c("b","QQ","a","A","bb")
# note: cat() produces no output for NULL[注:猫()不产生任何输出为NULL]
for(ch in ccc)
    cat(ch,":", switch(EXPR = ch, a=1,     b=2:3), "\n")
for(ch in ccc)
    cat(ch,":", switch(EXPR = ch, a=, A=1, b=2:3, "Otherwise: last"),"\n")

## Numeric EXPR does not allow a default value to be specified[#数字EXPR,不允许被指定默认值]
## -- it is always NULL[# - 它始终是NULL]
for(i in c(-1:3,9))  print(switch(i, 1,2,3,4))

## visibility[#能见度]
switch(1, invisible(pi), pi)
switch(2, invisible(pi), pi)

转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2025-1-24 05:25 , Processed in 0.034032 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表