R: How to Convert Character to Date Using Lubridate


You can use various functions from the lubridate package in R to convert a character column to a date format.

Two of the most common functions include:

  • ymd() – Convert character in year-month-date format to date
  • mdy() – Convert character in month-day-year format to date

The following examples show how to use the ymd() and mdy() functions in practice.

Note: Refer to the lubridate documentation for a complete list of functions you can use to convert characters to dates depending on the format your dates are in.

Example 1: Convert Character to Date Using ymd()

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(date=c('2022-01-05', '2022-02-18', '2022-03-21',
                        '2022-09-15', '2022-10-30', '2022-12-25'),
                 sales=c(14, 29, 25, 23, 39, 46))

#view data frame
df

        date sales
1 2022-01-05    14
2 2022-02-18    29
3 2022-03-21    25
4 2022-09-15    23
5 2022-10-30    39
6 2022-12-25    46

#view class of date column
class(df$date)

[1] "character"

Currently the values in the date column are characters, but we can use the ymd() function from the lubridate package to convert them to dates:

library(lubridate)

#convert character to date format
df$date <- ymd(df$date)

#view updated data frame
df

        date sales
1 2022-01-05    14
2 2022-02-18    29
3 2022-03-21    25
4 2022-09-15    23
5 2022-10-30    39
6 2022-12-25    46

#view updated class of date column
class(df$date)

[1] "Date"

We can see that the date column now has a class of Date instead of character.

Example 2: Convert Character to Date Using mdy()

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(date=c('March 4, 2022', 'April 9, 2022', 'May 6, 2022',
                        'May 29, 2022', 'June 1, 2022', 'July 2, 2022'),
                 sales=c(14, 29, 25, 23, 39, 46))

#view data frame
df

           date sales
1 March 4, 2022    14
2 April 9, 2022    29
3   May 6, 2022    25
4  May 29, 2022    23
5  June 1, 2022    39
6  July 2, 2022    46

#view class of date column
class(df$date)

[1] "character"

Currently the values in the date column are characters, but we can use the mdy() function from the lubridate package to convert them to dates:

library(lubridate)

#convert character to date format
df$date <- mdy(df$date)

#view updated data frame
df

        date sales
1 2022-03-04    14
2 2022-04-09    29
3 2022-05-06    25
4 2022-05-29    23
5 2022-06-01    39
6 2022-07-02    46

#view updated class of date column
class(df$date)

[1] "Date"

We can see that the date column now has a class of Date instead of character.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Convert Date to Numeric in R
How to Extract Month from Date in R
How to Add and Subtract Months from a Date in R

Leave a Reply

Your email address will not be published. Required fields are marked *