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.
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.
- Extract Day from Date in R
- Extract Year from Date in R
- Extract Year & Month from yearmon Object (zoo Package)
- as.Date Function in R
- R Programming Overview
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.
Statistics Globe Newsletter
10 Comments. Leave new
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.
Hi Oleg,
Thank you for your comment!
Could you elaborate how you have tried to find the correct number of words?
Regards,
Joachim
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
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:
I hope that helps!
Joachim
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.
Hey,
What exactly do you mean with “bin the observations”?
Do you want to calculate the mean by dates? In this case, you may have a look here.
Regards,
Joachim
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
Hey Taoridi,
You may use the month.abb object for this task. For example:
Regards,
Joachim
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?
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:
Regards,
Joachim