Search code examples
rposixctposixlt

How to convert date & time to numeric by using POSIXct and how to calculate the time?


Hi Friends Both these columns(starttime/stoptime) are in Character class ,how could I convert to numeric(POSIXct) to find the time consumption ,Thank you

starttime
12/31/2015 23:48
12/31/2015 23:47
12/31/2015 23:37
12/31/2015 23:37

stoptime
12/31/2015 23:51
12/31/2015 23:53
12/31/2015 23:43
1/1/2016 0:02


Solution

  • Additionally, you can also use mdy_hm function from lubridate -

    library(dplyr)
    library(lubridate)
    
    df <- df %>% mutate(across(c(starttime, stoptime), mdy_hm))
    

    In base R, you can use as.POSIXct

    df[1:2] <- lapply(df[1:2], as.POSIXct, format = "%m/%d/%Y %H:%M")