-1

I have a date column where each date is written without spaces and is in character format.

Date
<chr>
"20130402"
"20130403"
"20130404"

I want to convert these values to date, but it does not work.

I tried the following code to do so:

dataframe %>%
 as.Date(Date, origin = "2013-04-02") 

However, this resulted in wrong dates, such as "57128-06-03". Can someone help me on how can I fix this?

2 Answers2

1

Assuming the timezone is UTC and the format is year,month,day:

strptime("20130402", "%Y%m%d", tz = "UCT")
dario
  • 6,415
  • 2
  • 12
  • 26
0

An option with ymd

library(dplyr)
library(lubridate)
dataframe <- dataframe %>%
    mutate(Date = ymd(Date))
akrun
  • 874,273
  • 37
  • 540
  • 662