Extract Year & Month from yearmon Object in R (2 Examples)

 

In this tutorial you’ll learn how to return only the year and month from a zoo yearmon object in R.

The tutorial will consist of two examples for the extraction of years and months. To be more specific, the content looks as follows:

Let’s get started!

 

Example Data & Add-On Packages

First, we’ll have to construct some yearmon data that we can use in the following example code.

For this, we first have to install and load the zoo package:

install.packages("zoo")                       # Install & load zoo package
library("zoo")

Now, we can construct some example data in R:

my_date <- as.yearmon("Apr 2022", "%b %Y")    # Create example date
my_date                                       # Print example date
# "Apr 2022"

The previous output of the RStudio console shows that the example data is a single date (i.e. the month April of the year 2022).

Let’s check the data type of our example data by using the class function:

class(my_date)                                # Check class of example date
# "yearmon"

Our example data has the yearmon class. Looks good!

 

Example 1: Extracting Year & Month from yearmon Object Using format() Function

In Example 1, I’ll show how to extract the year and month of our yearmon object using the format function that is provided by the basic installation of the R programming language.

We can use the format function to return the year…

format(my_date, "%Y")                         # Extracting year
# "2022"

…the month formatted as name…

format(my_date, "%b")                         # Extracting month as name
# "Apr"

…the month formatted as number…

format(my_date, "%m")                         # Extracting month as character number
# "04"

…and the month formatted as numeric value:

as.numeric(format(my_date, "%m"))             # Extracting numeric month
# 4

Great – But in the next example I want to show you an alternative that is (in my opinion) even better than the R codes I have shown you before. So keep on reading!

 

Example 2: Extracting Year & Month from yearmon Object Using lubridate Package

Example 2 explains how to extract years and months from yearmon date objects using the functions of the lubridate package.

If we want to use the functions of the lubridate package, we first have to install and load lubridate:

install.packages("lubridate")                 # Install & load lubridate
library("lubridate")

Now, we can apply the year function of the lubridate package to extract the year of our data…

year(my_date)                                 # Applying year function
# 2022

…and the month function to extract the month of our data:

month(my_date)                                # Applying month function
# 4

Note that both outputs are automatically converted to the numeric class.

 

Video & Further Resources

Do you need more info on the R codes of this article? Then you might have a look at the following video of my YouTube channel. I show the examples of this article in the video:

 

 

In addition to the video, you may want to have a look at the other tutorials on this website. You can find some articles about dates in R below.

 

Summary: In this post, I showed how to extract years and months from yearmon objects in the R programming language. In case you have further questions, let me know 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.

Top