To parse a date from its string representation in R, we should use the lubridate package of the tidyverse collection. This package offers various functions for parsing a string and extracting the standard date from it based on the initial date pattern in that string. These functions are ymd()
, ymd_hm()
, ymd_hms()
, dmy()
, dmy_hm()
, dmy_hms()
, mdy()
, mdy_hm()
, mdy_hms()
, etc., where y, m, d, h, m, and s correspond to year, month, day, hours, minutes, and seconds, respectively.
For example, if we run the dmy()
function passing to it any of the strings “05-11-2023”, “05/11/2023” or “05.11.2023”, representing the same date, we’ll receive the same result: 2023-11-05
. This is because in all three cases, despite having different dividing symbols, we actually have the same pattern: the day followed by the month followed by the year.
Leave a Reply