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.

 

 

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.


2 Comments. Leave new

  • I would like to apply this “paste” command to a data frame. The reason I am seeking this is to convert the dates in my data frame from Chr to Date. But in my data frame, my dates are formatted YYYY-MM, and after running “as.date” to the $DATE column, I end up creating a whole field of NAs. Super frustrating. Here’s a sample data frame I created for this question:
    weather <- data.frame(date=c("2015-01", "2015-02", "2015-03", "2015-04"),
    rain=c(1.2, 3.2, 0.1, 0.5),
    maxT=c(75, 60, 70, 80))
    So far, if I apply the function as in your example,
    weather_new <- as.Date(paste(weather,"-01",sep=""))
    the result is [1] error: character string is not in a standard unambiguous format.

    Can you assist? It appears to me that the "paste()" only works on vectors, not data frames.

    Reply
    • Hey Michael!

      Yes, you’re right. The paste() function is designed to work with vectors and not with data frames. The error is due to you trying to paste a string into an entire data frame.

      What you want to do is apply the paste() function to the date column of your data frame and then use the as.Date() function to convert it to a Date class. Here’s how you can do that:

      Paste “-01” to the end of each date in the date column to ensure they are of the form “YYYY-MM-DD”.
      Convert that to a date.

      Here’s the code to achieve that:

      weather <- data.frame(date=c("2015-01", "2015-02", "2015-03", "2015-04"),
                            rain=c(1.2, 3.2, 0.1, 0.5),
                            maxT=c(75, 60, 70, 80))
       
      weather$date <- as.Date(paste(weather$date, "-01", sep=""))
      weather
      #         date rain maxT
      # 1 2015-01-01  1.2   75
      # 2 2015-02-01  3.2   60
      # 3 2015-03-01  0.1   70
      # 4 2015-04-01  0.5   80
       
      class(weather$date)
      # [1] "Date"

      Above, you can see how the date column is in the right format now.

      Best,
      Cansu

      Reply

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.

Top