How to Get the Month of a Date Object in R (Example Code)

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

Creation of Example Data

my_date <- as.Date(c("2021-09-07",    # Example dates in R
                     "2016-10-25",
                     "2020-03-08"))
my_date                               # Returning dates vector to console
# [1] "2021-09-07" "2016-10-25" "2020-03-08"

Example: Applying format Function to Get Only Months of Date Objects

format(my_date, "%m")                 # Extracting months of dates
# [1] "09" "10" "03"

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top