Convert Character String to Date Object in R (Example)


In this tutorial, I’ll explain how to change the character string format to the date class in R.

The article consists of one example for the conversion of characters to dates. More precisely, the content of the article is structured as follows:

Let’s just jump right in!


Creation of Example Data

Consider the following example data:

my_dates <- c("2020-01", "2015-10", "2033-05")         # Create example dates
my_dates                                               # Print example dates
# "2020-01" "2015-10" "2033-05"

The previous output of the RStudio console shows the structure of our example data – it’s a vector consisting of three values. The values have a year-month (or yyyymm) format.

Let’s check the class of our example data:

class(my_dates)                                        # Check class
# "character"

Our example data has the character class. This is quite often not the best option when working with dates.


Example: Convert Month/Year Character String to Date Object

The following code explains how to change the character class to the date class in R programming by using the as.Date and the paste functions.

my_dates_new <- as.Date(paste(my_dates,"-01",sep=""))  # Convert to date
my_dates_new                                           # Print updated data
# "2020-01-01" "2015-10-01" "2033-05-01"

We have stored the output in the data object my_dates_new. First, we have added the day (i.e. the first of each month) to our data (i.e. yyyymmdd format). Second, we have changed the converted the class of our data:

class(my_dates_new)                                    # Check class
# "Date"

Our updated data has the Date data type.


Video & Further Resources

Do you want to know more about the handling of date objects in R? Then you may watch the following video that I have published on my YouTube channel. I explain the R codes of this article in the video.


Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.


In addition, you might read the other articles that I have published on this website:


In this article, I explained how to convert character strings to the date class in R. If you have any further questions, tell me about it in the comments.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.