R中字符的逻辑运算符

Logical Operators on characters in R

我只是想知道 R 在应用逻辑运算符时如何评估不同的字符。喜欢

"a" > "b"
"b" > "c"
"a+b" > "b+a"
"-" > "+"
"&" > "%"

我只是想知道R是如何对不同的符号和字符进行升序或降序排列的?对于上面定义的每个操作,我之前的信念是 "NA"。但是对于每种情况,R returns 要么是 TRUE 要么是 FALSE。

查看帮助页面进行比较?Comparison

引用关键段落:

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. In Welsh ng may or may not be a single sorting unit: if it is it follows g. Some platforms may not respect the locale and always sort in numerical order of the bytes in an 8-bit locale, or in Unicode code-point order for a UTF-8 locale (and may not sort in the same order for the same language in different character sets). Collation of non-letters (spaces, punctuation signs, hyphens, fractions and so on) is even more problematic.

您可以通过显式请求字符或字符串值列表的顺序来解决这个难题...

items <- c('+','-','a','z', 'A', 'a+b', 'a+x', '*', '/')
items[order(items)]

sort可以得到订单

sort(c(letters, LETTERS, 0:9, "+", "\"", "¦", "@", "*", "#", "ç", "%", "&", "¬", "|",
"(", "¢", ")", "=", "?", "'", "´", "`", "^", "~", "!", "[", "]", "$", "£", "{", "}", 
".", ",", "-", "_", "<", ">", "/"))