Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close


These dates are stored as factors, which is okay unless we want to operate on them, e.g., calculating the difference between departure and arrival times. We could use what we know about working with strings to fix this problem, but it's easier and faster to use the lubridate package.

First, we want to change our data from the string to date format. Our dates in the departure_datetime column are currently formatted like this:

2016-11-28 11:25:34

First is the year, then the month, the day, and the time: hours, minutes, and seconds. lubridate provides helpful functions to change the date format. We want to use the ymd_hms() function. This stands for year, month, day, hour, minute, and second. Here's how it works:

trucks$departure_datetime <- ymd_hms(trucks$departure_datetime)

Note that the only argument is the vector of strings. Also, for this function to work properly, the vector of strings must contain strings in the year-month-day-time format. The function recognizes all non-alphanumeric separators, so a date could be in a completely messy format, like: 2016-11/28 11-25/34, and the ymd_hms() function would read it correctly.


Import the lubridate package. With the ymd_hms() function change the format of the column departure_datetime in the ships data frame. At the end, let's look again at the ships data frame using the str() function.