Why does “one” < 2 equal FALSE in R?

馋奶兔 提交于 2019-11-27 05:24:47
jdharrison

From help("<"):

If the two arguments are atomic vectors of different types, one is coerced to the type of the other, the (decreasing) order of precedence being character, complex, numeric, integer, logical and raw.

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.

OganM

It coerces 2 into a character, then it does an alphabetical comparison. And numeric characters are assumed to come before alphabetical ones

to get a general idea on the behavior try

'a'<'1'
'1'<'.'
'b'<'B'
'a'<'B'
'A'<'B'
'C'<'B'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!