Search code examples
rlubridateas.date

How to convert a "char" column to datetime column in large datasets


I am working with large datasets and in which one column is represented as char data type instead of a DateTime datatype. I trying it convert but I am unable to convert it.

Could you please suggest any suggestions for this problem? it would be very helpful for me

Thanks in advance

code which i am using right now

c_data$dt_1 <- lubridate::parse_date_time(c_data$started_at,"ymd HMS")

getting output:

2027- 05- 20 20:10:03 

but desired output is

2020-05-20 10:03

Solution

  • Here is another way using lubridate:

    library(lubridate)
    
    df <- tibble(start_at = c("27/05/2020 10:03", "25/05/2020 10:47"))
    
    df %>%
      mutate(start_at = dmy_hms(start_at))
    
    # A tibble: 2 x 1
      start_at           
      <dttm>             
    1 2020-05-27 20:10:03
    2 2020-05-25 20:10:47