例如,在Ruby中,只有nil和false是false.R中的内容是什么?
例如:5==TRUE并且5==FALSE两者都评估为FALSE.但是,1==TRUE是TRUE.关于(对象,数字等)评估的内容是否有任何一般规则?
Rei*_*son 49
这是记录在案的?logical.相关部分是:
Details:
‘TRUE’ and ‘FALSE’ are reserved words denoting logical constants
in the R language, whereas ‘T’ and ‘F’ are global variables whose
initial values set to these. All four are ‘logical(1)’ vectors.
Logical vectors are coerced to integer vectors in contexts where a
numerical value is required, with ‘TRUE’ being mapped to ‘1L’,
‘FALSE’ to ‘0L’ and ‘NA’ to ‘NA_integer_’.
Run Code Online (Sandbox Code Playgroud)
第二段有解释你所看到的,即行为5 == 1L和5 == 0L分别,这都应该回报FALSE,那里的1 == 1L和0 == 0L应该为TRUE,1 == TRUE和0 == FALSE分别.我相信这些都没有测试你想让他们测试的东西; 比较是基于R 的数值表示TRUE和FALSER,即在强制数字时它们采用的数值.
但是,只TRUE保证TRUE:
> isTRUE(TRUE)
[1] TRUE
> isTRUE(1)
[1] FALSE
> isTRUE(T)
[1] TRUE
> T <- 2
> isTRUE(T)
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
isTRUE是一个包装器identical(x, TRUE),从?isTRUE我们注意到:
Details:
....
‘isTRUE(x)’ is an abbreviation of ‘identical(TRUE, x)’, and so is
true if and only if ‘x’ is a length-one logical vector whose only
element is ‘TRUE’ and which has no attributes (not even names).
Run Code Online (Sandbox Code Playgroud)
因此,同样的优点,只FALSE保证完全相等FALSE.
> identical(F, FALSE)
[1] TRUE
> identical(0, FALSE)
[1] FALSE
> F <- "hello"
> identical(F, FALSE)
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
如果这涉及您,一定要使用isTRUE()或identical(x, FALSE)与检查等价TRUE和FALSE分别.==没有按照你的想法去做.
T和TRUE是 True,F和FALSE是 False。T但是, andF可以重新定义,因此您应该只依赖TRUE和FALSE。如果将 0 与 FALSE 和 1 与 TRUE 进行比较,您会发现它们也相等,因此您可能也认为它们是 True 和 False。
| 归档时间: |
|
| 查看次数: |
93299 次 |
| 最近记录: |