39  Dates with lubridate

Instead of defining Date and/or time formats using POSIX standard abbreviations, we can let the lubridate package do some guesswork for us, which works well most of the time.


Attaching package: 'lubridate'
The following objects are masked from 'package:base':

    date, intersect, setdiff, union
dt <- c("2020-03-04 13:38:54")
dt_posix <- as_datetime(dt)
dt_posix
[1] "2020-03-04 13:38:54 UTC"
class(dt_posix)
[1] "POSIXct" "POSIXt" 

Note that timezone defaults to UTC (Coordinated Universal Time) and must be set manually. PST is defined with “America/Los_Angeles” or the (officially deprecated) “US/Pacific” (tz database)

dt_posix <- as_datetime(dt, tz = "America/Los_Angeles")
dt_posix
[1] "2020-03-04 13:38:54 PST"
dt2 <- c("03.04.20 01:38.54 pm")
dt2_posix <- as.POSIXct(dt2, format = "%m.%d.%y %I:%M.%S %p")
dt2_posix
[1] "2020-03-04 13:38:54 PST"
dt2_posix <- as_datetime(dt2)
dt2_posix
[1] "2003-04-20 13:38:54 UTC"

dt2 got misinterpreted as year-month-day.
For these cases, lubridate includes a number of convenient functions to narrow down the guessing. The functions are named using all permutations of y, m, and d. The letter order signifies the order the information appears in the character you are trying to import, i.e. ymd, dmy, mdy, ydm, myd

dt2 <- c("03.04.20 01:38.54 pm")
dt2_posix <- mdy_hms(dt2, tz = "America/Los_Angeles")
dt2_posix
[1] "2020-03-04 13:38:54 PST"