I'm wondering why does date of character type still act as a Date type in R.
For example:
x <- "2020-01-15"
y <- "2020-01-29"
x < y // *returns true*
x >= 2020 // *returns true*
Is there some auto type conversion in the background?
CodePudding user response:
No, they are evaluated as character, and you can compare characters as well.
'a' < 'b'
# [1] TRUE
Note:
100 < 20
# [1] FALSE
'100' < '20'
# [1] TRUE
CodePudding user response:
Strings are compared lexicographicaly in R.
So the only reason x < y
is because the second last character of x
is 1
, but in y
it's 2
, so therefore x
is smaller than y
.
For string comparison of substrings of a string, i.e. 2020
is a substring of 2020-01-05
, the longer string would be the bigger string in lexicographical comparison. Therefore obviously the second case gives TRUE
.