Extract Dates from Time Series in R (2 Examples)

 

In this R post you’ll learn how to get the dates of a time series object.

The tutorial contains this content:

Here’s the step-by-step process:

 

Creation of Example Data

The following data is used as a basis for this R programming tutorial:

my_ts <- ts(1:108,                                # Create time series object
            start = 2020,
            frequency = 12)
my_ts                                             # Print time series object
#      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
# 2020   1   2   3   4   5   6   7   8   9  10  11  12
# 2021  13  14  15  16  17  18  19  20  21  22  23  24
# 2022  25  26  27  28  29  30  31  32  33  34  35  36
# 2023  37  38  39  40  41  42  43  44  45  46  47  48
# 2024  49  50  51  52  53  54  55  56  57  58  59  60
# 2025  61  62  63  64  65  66  67  68  69  70  71  72
# 2026  73  74  75  76  77  78  79  80  81  82  83  84
# 2027  85  86  87  88  89  90  91  92  93  94  95  96
# 2028  97  98  99 100 101 102 103 104 105 106 107 108

The previous output of the RStudio console shows the structure of our exemplifying data – It’s a time series containing monthly data for the years 202-2028.

Let’s check the class of our data:

class(my_ts)                                      # Check class of time series object
# [1] "ts"

We are dealing with a ts object.

In the following examples, I’ll explain how to extract only the dates from this ts object.

 

Example 1: Return Dates from Time Series Using as.yearmon() Function of zoo Package

In this section, I’ll demonstrate how to select only dates from a time series using the zoo package.

First, we need to install and load the zoo package:

install.packages("zoo")                           # Install zoo package
library("zoo")                                    # Load zoo package

Next, we can apply the as.yearmon function of the zoo package in combination with the time function as shown below:

dates_1 <- as.yearmon(time(my_ts))                # Extract dates
head(dates_1)                                     # Print head of dates
# [1] "Jan 2020" "Feb 2020" "Mar 2020" "Apr 2020" "May 2020" "Jun 2020"

As you can see, we have returned only the months and years of our time series.

 

Example 2: Return Dates from Time Series Using date_decimal() Function of lubridate Package

Example 2 shows how to use the lubridate package to extract dates from a time series object.

To be able to use the functions of the lubridate package, we first need to install and load lubridate:

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

Next, we can apply the date_decimal function of lubridate in combination with the as.numeric and time functions:

dates_2 <- date_decimal(as.numeric(time(my_ts)))  # Extract dates
head(dates_2)                                     # Print head of dates
# [1] "2020-01-01 00:00:00 UTC" "2020-01-31 11:59:59 UTC"
# [3] "2020-03-02 00:00:00 UTC" "2020-04-01 12:00:00 UTC"
# [5] "2020-05-01 23:59:59 UTC" "2020-06-01 12:00:00 UTC"

Once again, we have extracted the dates from our ts object. The output of the previous code has the POSIXct class, in contrast to the yearmon class of the output in Example 1.

 

Video, Further Resources & Summary

Have a look at the following video on my YouTube channel. In the video, I explain the R programming codes of this article.

 

 

Furthermore, you may have a look at some of the related articles on my website.

 

This article has explained how to return only the dates from a time series object in the R programming language. In case you have any additional questions, don’t hesitate to tell me about it in the comments section.

 

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