[R] What does "smaller than" comparison do on strings?

Duncan Murdoch murdoch.duncan at gmail.com
Wed May 25 16:35:59 CEST 2011


On 25/05/2011 6:06 AM, Niklaus Kuehnis wrote:
> What's the logic behind the following, and where can I find any
> documentation about it? In particular, why are 2:9 - as characters - not
> regarded as being smaller than 10?
>
> # R-Code:
> a<- as.character(1:12)
>
> a<  10
> #  [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> FALSE

See ?Comparison for help.  There are lots of details given there.

In summary:  your second comparison is of "2" to "10".  Since the 
character "2" sorts later than the character "1", "2" < "10" is FALSE.

Duncan Murdoch



More information about the R-help mailing list