I was shocked to see R act in the manner described below:
as.character(c(Sys.Date()))
#> [1] "2018-02-05"
as.character(list(Sys.Date()))
#> [1] "17567"
What causes this to occur? That is, it is obvious that the "17567" produced by as.integer(Sys.Date) is the outcome, but I do not see the reasoning behind why as.character(list(Sys.Date())) would ultimately call as.integer ().
(Usually, not specifying options(stringsAsFactors=FALSE) is to fault for strings being interpreted as integers, but that doesn't seem to be the case here.)
EDIT: Josh points out that this is caused by as.vector's underlying behaviour, but I don't think that makes much more sense:
17567 as.vector(Sys.Date())
"17567" returned by as.vector(Sys.Date(), "character").
Why? (Yes, I think the lower-level internals store dates as integers; but, this coercion to a literal integer in this situation without a warning seems odd to me.)
Additionally, this shows up in subtler ways:
tbl = "tibble:::as data frame(list(col1 = list(Sys.Date(), "stuff")")"
df = as.data.frame(tbl) df col1 col1 col1 1 17567 2 things
df[1, 1] #> [[1]]\s#> [1] "2018-02-05"
It should be noted that the date is still the date even though the print method for data.frame displays it as an integer even though it is actually a list column.
It's unclear why the print method in this instance displays the data in such a misleading manner or what is going on with it.
EDIT:
Other instances when the Date class unexpectedly fails, revealing the underlying numeric data