Table of Contents
Lubridate is a popular package in R that allows for easy manipulation and handling of dates and times. To convert a character to a date using lubridate, one can use the `ymd()` function. This function takes in a character string and converts it to a date using the year, month, and day specified in the string. This simplifies the process of converting characters to dates and avoids potential errors that may arise with other methods. Additionally, lubridate offers various other functions for converting characters to dates, such as `dmy()` and `mdy()`, making it a versatile and reliable tool for date manipulation in R.
R: 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 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.
Cite this article
stats writer (2024). How can I convert a character to a date using lubridate?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-a-character-to-a-date-using-lubridate/
stats writer. "How can I convert a character to a date using lubridate?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-a-character-to-a-date-using-lubridate/.
stats writer. "How can I convert a character to a date using lubridate?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-a-character-to-a-date-using-lubridate/.
stats writer (2024) 'How can I convert a character to a date using lubridate?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-a-character-to-a-date-using-lubridate/.
[1] stats writer, "How can I convert a character to a date using lubridate?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I convert a character to a date using lubridate?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
