I'm trying to convert a column of dates into strings, because I want to use them as factor levels at some later point in my code.
The date column is part of a tibble, and is of class Date. I figured that a simple as.character() conversion would do the trick, but unfortunately I was wrong. Instead of neatly formatted strings it returns a number in string form. For example today (22 november 2017) would come out as "17492". So somewhere in the process the date gets converted into its numeric format and only then turned into a character string.
Now I did find a workaround, by unlisting the data, converting it again to dates and then to character strings, but it is fairly inefficient.
Can anyone explain i) why this occurs and ii) if there is an easier fix?
Below a reproducible example:
#Get current system date
foo <-Sys.Date()
#Convert to list
foo <- as.list(foo)
#The following then produces the number string:
as.character(foo)
[1] "17492"
#The following code works but is a rather annoying work-around
as.character(as.Date(unlist(foo), origin=as.Date("1970-01-01")))
[1] "2017-11-22"