One option is parse_date_time
from lubridate
to match multiple patterns and convert it to a datetime class
library(lubridate)
parse_date_time(Datime$Day, c("%d/%m/%Y %H:%M", "%d-%m-%Y %H:%M:%S"))
#[1] "2016-07-11 11:51:00 UTC" "2016-07-11 17:57:00 UTC"
#[3] "2016-07-12 09:17:00 UTC" "2016-07-12 21:08:00 UTC"
#[5] "2016-07-13 08:33:16 UTC" "2016-07-13 21:57:28 UTC"
#[7] "2016-07-14 06:15:32 UTC" "2016-07-15 05:11:52 UTC"
#[9] "2016-07-15 17:57:27 UTC"
Another option is anytime
(if the formats are not already found in the default format list getFormats()
, add the new format with addFormats()
)
library(anytime)
addFormats( c("%d/%m/%Y %H:%M", "%d-%m-%Y %H:%M:%S"))
anytime(Datime$Day)
data
Datime <- structure(list(Day = c("11/7/2016 11:51", "11/7/2016 17:57",
"12/7/2016 9:17", "12/7/2016 21:08", "13-07-2016 08:33:16",
"13-07-2016 21:57:28",
"14-07-2016 06:15:32", "15-07-2016 05:11:52", "15-07-2016 17:57:27"
)), .Names = "Day",
class = "data.frame", row.names = c(NA, -9L
))