0

So here is what I found in R:

>   "20" < 3
[1]  TRUE
>   "20" < 2
[1]  FALSE

and I tried to find out what exactly number the "20" is. So basically, "20" is between 2.99999999999999489 and 2.99999999999999490, so why is it?

Also, "200" and "2000" (and so on...) have the same range as "20", but the wired thing is R gives me this:

>  "20" == "200"
[1]  FALSE
>  "200" == "2000"
[1]  FALSE

I believe they are not same, but R stores them in different number with that range "20" has. So beside why is it, I'm more curious about how R stores characters in numbers, I mean if "200" and "2000" are different numbers between 2.99999999999999489 and 2.99999999999999490, then how many numbers between them? (I know real numbers are infinite, but there must be a digit that R decides to end with)

  • 1
    Comparisons of strings are done character-by-character until inequality is found. That is, `"2" > "10"` is true because `"2" > "1"` is true, and it stops. In your first case, `"2"` is compared with `"3"` and found to be "less than", so it stops there with true. In your second case, `"20" < 2`, it checks `"2"` and `"2"` and finds them equal, then checks `"0"` against ``, which is not true, so it returns false (no more comparisons are possible). – r2evans Jan 20 '22 at 04:52

1 Answers1

1

The help for < tells:

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 when you do "20" < 3, R internally converts 3 to "3". So this becomes "20" < "3". The comparison between strings is lexicographic (same as the order of words in dictionaries).

Kota Mori
  • 6,510
  • 1
  • 21
  • 25