Neeraj
Neeraj

Reputation: 1236

Logical Operators on characters in R

I just wanted to know how R evaluate different characters when applied logical operators. Like

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

I just wanted to know how R arrange different symbols and characters in ascending or descending order? My prior belief was "NA" for each operation defined above. But R returns either TRUE or FALSE for each and every case.

Upvotes: 2

Views: 1262

Answers (3)

Benjamin Schlegel
Benjamin Schlegel

Reputation: 527

With sort you can get the order.

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

Upvotes: 1

clancy
clancy

Reputation: 192

You can solve this conundrum by explicitly requesting the order of a list of char or string values...

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

Upvotes: 2

Stephen Henderson
Stephen Henderson

Reputation: 6532

See the help page for Comparison ?Comparison

To quote the key passage:

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.

Upvotes: 2

Related Questions