# Convert year-month-day hour-min-sec
x <- c("09-02-03 01:11:11", "09-02-03 02:11:11","09-02-03 02:25:11")
format_time(x)
# Convert day-month-year hour-min, and use a separator in the format
x <- c("03-02-09 01:11", "03-02-09 02:11","03-02-09 02:25")
format_time(x, format = "dmy_HM")
# Convert when AM/PM is present
x <- c("09-02-03 11:11:11 AM", "09-02-03 12:11:11 PM","09-02-03 01:25:11 PM")
# This is WRONG - the AM/PM indicator is missing
format_time(x, format = "dmyHMS")
# This is correct
format_time(x, format = "dmyHMSp")
# Convert dataframe with year-month-day hour-min-sec (ymdHMS default)
x <- data.frame(
x = c("09-02-03 01:11:11", "09-02-03 02:11:11","09-02-03 02:25:11"),
y = c(23, 34, 45))
format_time(x, time = 1)
# Convert dataframe with time in a different column and non-default format
x <- data.frame(
x = c(23, 34, 45),
y = c("09-02-2018 11:11:11 AM", "09-02-2018 12:11:11 PM","09-02-2018 01:25:11 PM"),
z = c(56, 67, 78))
format_time(x, time = 2, format = "dmyHMSp")
# Convert dataframe with separate date and time columns, and times crossing midnight
x <- data.frame(
w = c("09-02-18", "09-02-18","10-02-18"),
x = c("22:11:11", "23:11:11","00:25:11"),
y = c(23, 34, 45),
z = c(56, 67, 78))
# Crosses midnight, but parses correctly even without dates
format_time(x, time = 2, format = "HMS")
# Include dates to double check
format_time(x, time = 1:2, format = "dmyHMS")
# Input same as different column order & appropriate format order
format_time(x, time = 2:1, format = "HMSdmy")
# Convert a data frame with date and time split over multiple columns
x <- data.frame(
u = c("09", "09","10"),
v = c("02", "02","02"),
w = c("2018", "2018","2018"),
x = c("22:11:11", "23:11:11","00:25:11"),
y = c(23, 34, 45),
z = c(56, 67, 78))
format_time(x, time = 1:4, format = "dmyHMS")
Run the code above in your browser using DataLab