Extract Month from Date in R (Example)

 

In this R post you’ll learn how to return only the months of a date vector.

Table of contents:

Let’s just jump right in:

 

Construction of Example Data

Have a look at the example data below:

x <- "2022-10-05"                                 # Create example date
x                                                 # Print date
# "2022-10-05"

As you can see based on the previous output of the RStudio console, our example data object contains a single date formatted as a character string.

 

Example: Extracting Month from Date Object Using as.Date() & format() Functions

This example shows how to extract the month of a date in R. For this, we first have to convert our data object to the Date class using the as.Date function:

x_formatted <- as.Date(x, format = "%Y-%m-%d")    # Convert character to Date
x_formatted                                       # Print Date object
# "2022-10-05"

The formatted data object looks exactly the same as the original data. However, we can see the difference when checking the class:

class(x_formatted)                                # Checking class
# "Date"

Our new data object has the data type Date.

Now, we can use the format function to extract only the month from our object with the data type Date:

x_month <- format(x_formatted, "%m")              # Extract month
x_month                                           # Print month
# 10

The month of our date is October (i.e. the value 10).

 

Video, Further Resources & Summary

I have recently released a video on my YouTube channel, which illustrates the examples of this article. You can find the video below:

 

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.

 

Furthermore, I can recommend reading some of the other articles of this homepage.

 

In summary: At this point you should know how to get the month from an object with the class Date in R programming. In case you have further questions, tell me about it in the comments section below.

 

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.


10 Comments. Leave new

  • Oleg Kontchaev
    March 1, 2021 12:26 am

    Dear Joachim, I have a practical question related to one task in my Data Science course. It sounds simple: “Use the gutenberg_download() function to download the text for Pride and Prejudice. Use the tidytext package to create a tidy table with all the words in the text. Save this object as words.
    How many words are present in the book?” My answers 122342 or 122217 or 122214 or 122189 or 6492 were not accepted as the right answer. Please, let me know if you can take a look at this task.

    Reply
  • Augustine Conteh
    February 7, 2022 12:00 pm

    Hi Joachim,
    thanks for your video. Really great.
    I have a data file with dates and the header is called sale dates of 100 time series.
    Should I replace sale date as “x “to perform the procedures you just did?
    For example
    Sale date
    2022/01/01
    2022/03/01
    Thanks

    Reply
    • Hey Augustine,

      Thank you very much, glad you like it!

      Below, you can find another example that tries to reproduce your situation. It shows how to extract the month values from an entire data frame column:

      data <- data.frame(Sale.date = as.Date(c("2022/01/01",
                                               "2022/03/01"),
                                             format = "%Y/%m/%d"))
      data
      #    Sale.date
      # 1 2022-01-01
      # 2 2022-03-01
       
      format(data$Sale.date, "%m")
      # [1] "01" "03"

      I hope that helps!

      Joachim

      Reply
  • I have a similar data set where each observation has the date recorded. I want to bin the observations based on the month variable that I created.

    How would you do this? I keep trying and get an error saying my ‘x’ must be numeric.

    Reply
  • I have a similar data set except I want the month to display as january, february instead of 01, 02. How do i go about that

    Reply
    • Hey Taoridi,

      You may use the month.abb object for this task. For example:

      month.abb[c(1, 2)]
      #[1] "Jan" "Feb"

      Regards,
      Joachim

      Reply
  • Hello,
    Thank you! your videos are great! but what if I have a data set with multiple dates and I only want to extract the month of July?

    Reply
    • Hey Nataly,

      Thank you for the kind words, glad you find my videos helpful!

      You can subset dates based on logical conditions. Have a look at the following example code:

      x <- seq(as.Date("2020/10/05"), by = "day", length.out = 10)
      x
      # [1] "2020-10-05" "2020-10-06" "2020-10-07" "2020-10-08" "2020-10-09" "2020-10-10" "2020-10-11" "2020-10-12" "2020-10-13" "2020-10-14"
       
      x_new <- x[x > as.Date("2020-10-06") & x < as.Date("2020-10-12")]
      x_new
      # [1] "2020-10-07" "2020-10-08" "2020-10-09" "2020-10-10" "2020-10-11"

      Regards,
      Joachim

      Reply

Leave a Reply

Your email address will not be published.

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

Menu
Top