'Convert char date AM/PM to POSIXct

I have a column dataframe (sleep$Time) with a format like this:

time <- data.frame(sleep = c("4/27/2016 12:00:00 AM",
                             "4/12/2016 12:00:00 AM",
                             "5/16/2016 12:00:00 AM",
                             "5/11/2016 12:00:00 AM"))

There are around 300 rows with a character format.

I have try:

sleep$Time <- parse_date_time(sleep$Time, "%m/%d/%Y %I:%M:%S %p") #date is returned as POSIXct but the time is gone

sleep$Time <- as.POSIXct(sleep$Time, format="%m/%d/%Y %I:%M:%S %p") #date is returned as POSIXct but the time is gone

sleep$Time <- strptime(sleep$SleepDay, format = "%m/%d/%y %I:%M:%S %p") #return NA

Is there any other way that I can try?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source