Home » excel » Why is Male > Female equal to TRUE in R, Excel, and VBA? How about in other languages?

Why is Male > Female equal to TRUE in R, Excel, and VBA? How about in other languages?

Posted by: admin March 9, 2020 Leave a comment

Questions:

I’m currently taking the course: Introduction to R on DataCamp and in one exercise (Battle of the sexes) there is an instruction like this:

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

The above instruction inspired me to test the following code in RStudio:

'Male' > 'Female'

To my surprise, R gave me the output TRUE! I also tried in Excel and VBA, and both came up with outputs TRUE, too! Now, I begin to think that they’re programming languages with sexist views (Just kidding, hehe…).

enter image description here

So I wonder, what really happened here? Could anyone here explain it to me? Does this hold TRUE, too, for other programming languages? Why?

How to&Answers:

For R, see help('>') or its documentation here, and the wikipedia link about 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.”

So summarizing; in your locale, the value of ‘F’ is smaller than the value of ‘M’ in the collation sequence, and thus Mxxx is larger than Fyyy.

Answer:

In other languages like C# you can’t compare strings with

   "Male" > "Female"

Answer:

VBA for example converts the first Letter to ASCII and then compares it.

MsgBox Asc("male") '= 109
MsgBox Asc("female") '= 102
MsgBox Asc("Male") '= 77
MsgBox Asc("Female") '= 70

This is why it says "male" > "female" is true. But "Male" > "female" is false.

For the other languages it will be similiar

Answer:

In “less flexible” programming languages, you can’t use the “>” or “<“ operators to compare strings.

In “more flexible” programming languages such as VBA where you can write:

b = “3”
a = 5 + b
>> a = 8 (implicit conversion of string to number)

… you get to evaluate strings by ordinals (numeric values associated to letters in ASCII tables) when applying the larger or smaller operator. And since “M” in the alphabet comes after than “F” (having a higher ordinal), the strings comparison gives you that result.

If you want it more feminist, you can compare “Madame” (woman in French) and “Hombre” (man in Spanish) 🙂