Bug 15690 - Comparsion of numbers and characters
Summary: Comparsion of numbers and characters
Status: CLOSED WONTFIX
Alias: None
Product: R
Classification: Unclassified
Component: Low-level (show other bugs)
Version: R 3.0.2
Hardware: Other Other
: P5 normal
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2014-02-26 11:49 UTC by timow
Modified: 2015-04-02 12:06 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description timow 2014-02-26 11:49:11 UTC
R is capable of comparing numbers and numeric characters, however does it in a strange and unintuitive way:

> 3 > 2
[1] TRUE
> 3 > "2"
[1] TRUE
> 3 > "3"
[1] FALSE
> 3 > "4"
[1] FALSE
> 3 > "20"
[1] TRUE
> 3 > "30"
[1] FALSE
> 3 > "a"
[1] FALSE

It is easy to imagine "how" and "why" R does so, however this can lead to some serious problems if one doesn't expect this kind of behavior.

Example of problematic situation: you have a table of counts (table() command on some variable), since column names are 1...n, you can use the column names as numeric values, e.g. comparing if last column name is smaller than some value. If you forget to change the names into numeric format, it will work only for numbers from 1 to 9.

In my opinion it should have worked like one this:
a) R don't allows for number vs. character comparsion, or
b) it takes more "thought" while doing this, and/or
c) prints a warning message while doing so.
Comment 1 Duncan Murdoch 2014-02-26 11:59:35 UTC
This is documented behaviour (see ?">").  Users should expect R to act as documented.