Use the following methods to cast string column values to dates in R:
- Using r-base as.Date function:
as.Date (my_df$my_col)
- Use the lubridate library:
library (lubridate)
dates_l <- ymd(my_df$my_col)
Convert column values to dates in R [Detailed Example]
Create DataFrame
We will start by initializing a simple DataFrame using the following R code:
interviews <- c (115, 17, 25, 34, 45)
dates <- c ('2023-12-28', '2023-12-29', '2024-01-01', '2024-01-02',
'2024-01-03')
hr <- data.frame (dates = dates, interviews=interviews)
We can now use the str() function to find the data types of each column in the hr DataFrame:
str(hr)
As can’t be seen, the dates column is made of strings/ characters (chr):
'data.frame': 5 obs. of 2 variables: $ dates : chr "2023-12-28" "2023-12-29" "2024-01-01" "2024-01-02" ... $ interviews: num 115 17 25 34 45
Cast column to dates
As mentioned earlier, there are two key ways to approach this challenge:
Using base R as.Date() function:
dates <- as.Date( c ('2023-12-28', '2023-12-29', '2024-01-01', '2024-01-02',
'2024-01-03'))
Using the lubridate library:
As the string format in the dates vector is yyyy-mm-dd, we will use the ymd function:
library (lubridate)
dates <- lubridate::ymd( c ('2023-12-28', '2023-12-29', '2024-01-01', '2024-01-02',
'2024-01-03'))
Note: you can use additional lubridate functions (such as myd or dmy) to convert the string values in the column to datetimes, depending of the string format. If the function chosen doesn’t fit the string format you will get the following error:
Warning message:
All formats failed to parse. No formats found.
Additional Note: Make sure to install the tidyverse library (which includes lubridate, as well as ggplot2 and dplyr) before invoking the lubridate library.