本文介绍了为什么“一个" < R中等于2 FALSE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Hadley Wickham的有关胁迫的Advanced R部分,但我无法理解此比较的结果:

I'm reading Hadley Wickham's Advanced R section on coercion, and I can't understand the result of this comparison:

"one" < 2
# [1] FALSE

我假设R将2强制转换为字符,但是我不明白为什么R返回FALSE而不是返回错误.自此以来,这尤其让我感到困惑.

I'm assuming that R coerces 2 to a character, but I don't understand why R returns FALSE instead of returning an error. This is especially puzzling to me since

-1 < "one"
# TRUE

所以我的问题有两个:首先,为什么要回答这个问题,其次,有没有办法看到R如何像这些示例一样在逻辑向量中转换单个元素?

So my question is two-fold: first, why this answer, and second, is there a way of seeing how R converts the individual elements within a logical vector like these examples?

推荐答案

来自help("<"):

因此,在这种情况下,数字的优先级低于字符.因此,2被强制为字符"2".字符向量中字符串的比较是按字典顺序进行的,据我所知,它是按字母顺序排列的,但与语言环境有关.

So in this case, the numeric is of lower precedence than the character. So 2 is coerced to the character "2". Comparison of strings in character vectors is lexicographic which, as I understand it, is alphabetic but locale-dependent.

这篇关于为什么“一个" &lt; R中等于2 FALSE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:44