I recently came across R code that compared a POSIXlt date object with a date string.
as.POSIXlt.date("2007-02-02") >= "2007-02-01"
[1] FALSE
The result, surprisingly at least for me, was FALSE. I was expecting that the POSIXlt object would be coerced in to a character vector and so the inequality should test TRUE. I then tried explicit coercion and coercing either side in to the other's type yielded true.
as.character(as.POSIXlt.date("2007-02-02")) >= "2007-02-01"
[1] TRUE
and
as.POSIXlt.date("2007-02-02") >= as.POSIXlt.date("2007-02-01")
[1] TRUE
I think coercing the LHS date object to a character vector is semantically wrong because the comparison then would be lexicographic which is not what is intended (although it evaluates to TRUE in this case). Am I right?
In my opinion third expression is semantically right code. But why does the first code not work (it evaluates to FALSE)? Doesn't R coerce both sides to character vectors before comparing them?
Here's my platform information:
R version 3.1.0 (2014-04-10) -- "Spring Dance"
Platform: x86_64-redhat-linux-gnu (64-bit)
I am new to R. Any help is much appreciated.
Thanks, Farhan