Why does as character return an integer on a list of dates

0 votes
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
Jul 18, 2022 in Data Analytics by avinash
• 1,840 points

edited Mar 4 20 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP