• Post author:
  • Post category:R Programming
  • Post last modified:March 27, 2024
  • Reading time:8 mins read
You are currently viewing How to Convert Character to Date in R?

How to convert a Character (string) column to a Date column in an R data frame? You can use the as.Date() function and the lubridate package in R to convert the string format into a date format based on the given string format. Managing dates and times in R is crucial for various data analysis tasks, including time series analysis, data visualization, and data manipulation.

In this article, I will explain how to convert string columns from character to date format using the as.Date() function and the lubridate package.

Key points-

  • Use the as.Date() function in R to convert character columns to date format.
  • By default, as.Date() returns date in “yyyy-mm-dd” format.
  • Specify input format using the format parameter in as.Date() for custom date formats.
  • The lubridate package offers additional functions like ymd() and mdy() for conversion.
  • ymd() function converts character dates in “year-month-date” order.
  • mdy() function converts character dates in “month/day/year” order.
  • Before conversion, ensure the correct order of the date elements in the input string.
  • Check data types after conversion using class().

Let’s create a data frame with a character column of dates in the format of yyyy/mm/dd then apply various techniques of R and get the desired results.


# Create a data frame
df <- data.frame(
  char_date = c("2024/03/15", "2024/04/20", "2024/09/30", "2024/05/05", "2024/06/11")

print("Original Data Frame:")
print(df)
print("Check the data types before conversion:")
class(df$char_date)

Yields below output.

convert character to date

To convert a Character column to a Date column in the R data frame, you can use the as.Date() function. By default, it returns a date format in the form of “yyyy-mm-dd”. class() function is used to check the data types of the data frame’s column.


# Convert the string column from character to date 
df$date <- as.Date(df$char_date)
print("Updated Data Frame:")
print(df)
print("Check the data types after conversion:")
class(df$date)
 

The above code has returned an updated data frame where the column of the original data frame is converted from string format to date format.

Yields below output.

convert character to date

Altermatively, you can use the format parameter of as.Date() function to specify the input format of the given string. Let’s use the format parameter to convert the character column of the dates into date format.


# Convert string column to date column
# Using format param
# Create Data Frame
df <- data.frame(
  char_date = c("Mar 15, 2024", "April 20, 2024", "July 30, 2024", "May 5, 2024", "June 11, 2024"))
df$date <- as.Date(df$char_date, format = "%b %d, %Y")
print("Updated DataFrame:")
print(df)
print("Check the data types after conversion:")
class(df$date)

In the above example, when we check the data type of the character column of the data frame it has a date type instead of a character type.

Yields below output.


# Output:
[1] "Updated DataFrame:"
       char_date       date
1   Mar 15, 2024 2024-03-15
2 April 20, 2024 2024-04-20
3  July 30, 2024 2024-07-30
4    May 5, 2024 2024-05-05
5  June 11, 2024 2024-06-11

[1] "Check the data types after conversion:"
[1] "Date"

Using R lubridate Package to Convert Character to Date

You can also use the ymd() and the mdy() functions of the lubridate package in R to convert a string column from a character to a date format. If you want to use these functions you need to install the lubridate package in the R console as install.packages("lubridate"). After completing the installation you need to load it as library(lubridate).

Convert Date using ymd() Function

You can use the ymd() function of the lubridate package to convert characters format to date format. Make sure you can follow the order of date characters that you can use as an input string as year-month-date.


# Convert character to date using ymd() function
library(lubridate)
df <- data.frame(
  char_date = c("2024/03/15", "2024/04/20", "2024/09/30", "2024/05/05", "2024/06/11")
)
print("Data Frame:")
print(df)
print("Check the data types before conversion:")
class(df$char_date)
# Convert character to date format
df$date <- ymd(df$char_date)

print("Updated Data Frame:")
print(df)
print("Check the data types after conversion:")
class(df$date)

Yields below output.


# Output:
[1] "Data Frame:"
   char_date
1 2024/03/15
2 2024/04/20
3 2024/09/30
4 2024/05/05
5 2024/06/11
"Check the data types before conversion:"
[1] "character"

> # Convert character to date format
[1] "Updated Data Frame:"
   char_date       date
1 2024/03/15 2024-03-15
2 2024/04/20 2024-04-20
3 2024/09/30 2024-09-30
4 2024/05/05 2024-05-05
5 2024/06/11 2024-06-11
"Check the data types after conversion:"
[1] "Date"

Convert Date Type using mdy() Function

Similarly, you can use the mdy() function of the lubridate package to convert characters to date format. You must specify the order of the input string of date characters in the form of month/date/year.


# Convert character to date using mdy() function
library(lubridate)
df <- data.frame(
    char_date = c("Mar 15, 2024", "April 20, 2024", "July 30, 2024", "May 5, 2024", "June 11, 2024"))
print("Data Frame:")
print("Check the data types before conversion:")
class(df$char_date)

# Convert character to date format
df$date <- mdy(df$char_date)
print("Updated Data Frame:")
print(df)
print("Check the data types after conversion:")
class(df$date)

Yields below output.


# Output:
[1] "Data Frame:"
       char_date
1   Mar 15, 2024
2 April 20, 2024
3  July 30, 2024
4    May 5, 2024
5  June 11, 2024
"Check the data types before conversion:"
[1] "character"

> # Convert character to date format
[1] "Updated Data Frame:"
       char_date       date
1   Mar 15, 2024 2024-03-15
2 April 20, 2024 2024-04-20
3  July 30, 2024 2024-07-30
4    May 5, 2024 2024-05-05
5  June 11, 2024 2024-06-11
"Check the data types after conversion:"
[1] "Date"

Conclusion

In this article, I have explained the as. Date() function and using its format parameters and usage how we can convert the string column of the R data frame from character type to date type. Also explained using the lubridate package function how we can take the input string and convert string format to date format.

Happy Learning!!

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.