为什么在R,Excel和VBA中男性>女性等于TRUE?用其他语言怎么样?

我正在学习这门课程:介绍R在DataCamp和一次练习(性别之战)有一个这样的指示:

Read the code in the editor and click ‘Submit Answer’ to test if male
is greater than (>) female

上面的指令激发了我在RStudio中测试以下代码:

'Male' > 'Female'

令我惊讶的是,R给了我输出TRUE!我也试过Excel和VBA,两者都提出了输出TRUE!现在,我开始认为他们是用性别歧视观点编程的语言(开玩笑,呵呵……).

enter image description here

所以我想知道,这里到底发生了什么?有谁能在这里向我解释一下吗?对于其他编程语言,这也适用于TRUE吗?为什么?

最佳答案
对于R,请参阅帮助(‘>’)或其文档here,以及关于collation的维基百科链接:

“Comparison of strings in character vectors is lexicographic within the
strings using the collating sequence of the locale in use: see
locales. The collating sequence of locales such as en_US is normally
different from C (which should use ASCII) and can be surprising.
Beware of making any assumptions about the collation order: e.g. in
Estonian Z comes between S and T, and collation is not necessarily
character-by-character – in Danish aa sorts as a single letter, after
z.”

总结一下;在您的语言环境中,“F”的值小于归类序列中的“M”值,因此Mxxx大于Fyyy.

转载注明原文:为什么在R,Excel和VBA中男性>女性等于TRUE?用其他语言怎么样? - 代码日志