Home > Enterprise >  How to convert "numeric string" to a date object using `as.Date()`?
How to convert "numeric string" to a date object using `as.Date()`?

Time:05-12

I have a list of calendar dates in a data frame given in the format:

20000110 
20000117 
20000124

which standard for 2000-01-10, 2000-01-17, 2000-01-24, etc.

However, using

df %>% mutate(date= as.Date(df$data, format = "%Y%m%d") )

does not work. It springs an error asking me to supply an origin, which is an optional input for as.Date(). If I then supply the first date in my list as the origin, it gives me completely wrong dates:

df %>% mutate(date= as.Date(df$data, format = "%Y%m%d", origin="20000110") )

CodePudding user response:

I found the answer. The trick is to use the lubridate library's ymd() function:

df %>% mutate(date= lubridate::ymd(df$data) )

This works perfectly!

CodePudding user response:

Your date is of class "numeric", so as.Date.numeric is dispatched which has origin= as second argument. So all you have to do is coerce as.character to dispatch as.Date.character, which has the desired format= argument.

transform(df, date=as.Date(as.character(date), '%Y%m%d'))
#         date
# 1 2000-01-10
# 2 2000-01-17
# 3 2000-01-24

Normally the origin= is January 1, 1970, the so-called Unix epoch, and a numeric "date" is the number of days since then (in "POSIXt" format seconds).

as.Date(as.numeric(Sys.Date()))
# Error in as.Date.numeric(as.numeric(Sys.Date())) : 
#   'origin' must be supplied

as.Date(as.numeric(Sys.Date()), origin='1970-01-01')
# [1] "2022-05-12"

Obviously this wouldn't make sense with your kind of numerics.


Data:

df <- structure(list(date = c(20000110, 20000117, 20000124)), class = "data.frame", row.names = c(NA, 
-3L))
  • Related