Convert Data Frame with Date Column to Time Series Object in R (Example)

 

In this article you’ll learn how to change the data frame class to the xts / zoo data type in the R programming language.

Table of contents:

Let’s take a look at some R codes in action!

 

Creation of Example Data

We use the following data as basement for this R tutorial:

data <- data.frame(date = c("2020-10-01",    # Create example data
                            "2021-07-08",
                            "2018-01-18",
                            "2018-05-05",
                            "2025-12-10"),
                   value = 1:5)
data                                         # Print example data
#         date value
# 1 2020-10-01     1
# 2 2021-07-08     2
# 3 2018-01-18     3
# 4 2018-05-05     4
# 5 2025-12-10     5

Have a look at the previous output of the RStudio console. It shows that our example data has two columns. The first variable contains dates formatted as character strings and the second variable contains some randomly selected values.

We can also check the class of our data:

class(data)                                  # Check class of data
# "data.frame"

As you can see, our example data has the data.frame class.

 

Example: Converting Data Frame to xts / zoo Object

This Example illustrates how to switch from data.frame class to a time series object (i.e. xts or zoo). First, we have to convert our character string variable to the Date class.

data$date <- as.Date(data$date)              # Convert character string column to date

We also need to install and load the xts package:

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

We can use the xts function provided by the xts package to convert our data frame to a time series object as shown below:

data_ts <- xts(data$value, data$date)        # Convert data frame to time series
data_ts                                      # Print time series
#            [,1]
# 2018-01-18    3
# 2018-05-05    4
# 2020-10-01    1
# 2021-07-08    2
# 2025-12-10    5

Let’s check the data type of our updated data object:

class(data_ts)                               # Check class of time series
# "xts" "zoo"

As you can see, we switched the class from data.frame to xts / zoo.

 

Video, Further Resources & Summary

In case you need further explanations on the R programming code of the present tutorial, you could have a look at the following video of my YouTube channel. In the video, I show the content of this article in a live session.

 

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.

 

In addition, you might want to have a look at the related posts of this website. I have released several articles already:

 

In summary: In this tutorial you learned how to convert data frames to times series objects in the R programming language. In case you have additional questions, please 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.


10 Comments. Leave new

  • Abujam Manglem singh
    July 14, 2021 10:11 am

    I have yearly data for monthly temperature in celcius like:

    Year. Jan. Feb. Mar. Ap.
    2001. 5. 6
    2002 6 8
    2003. 7. 8
    2004. 5. 5.5
    2005 4.5. 6
    Etc. Etc. Etc

    How do i covert this data into time series so that ARIMA model can be use for forecasting values for a given month

    Reply
    • Hey Abujam,

      I’m sorry for the late response, I just came back from holidays and did not have the chance to read your message earlier.

      Are you still looking for a solution to this problem?

      Regards

      Joachim

      Reply
  • I have monthly data for sales of different stores like:
    store_code. year. month. sales. city …
    001. 2010. 1. 8899. ny
    001. 2010. 2. 8334. ny
    001. 2011 1. 8299. ny
    002. 2010. 1. 1399. la

    How can i convert this data into time series so that one ARIMA model can be built for forecasting values for all stores? Thanks in advance

    Reply
  • Hello Joachim
    I have data frame with 16 rows and 16383 columns and covert to create ts object using the date vector.
    I created d <- c("2000-03-01","2001-03-01","2002-03-01","2003-03-01","2004-03-01","2005-03-01","2006-03-01","2007-03-01","2008-03-01","2009-03-01","2010-03-01","2011-03-01","2012-03-01","2013-03-01","2014-03-01","2015-03-01")
    d <- as.Date(d)
    Not sure how I combine my with my dates.
    Really appreciate if you can help me

    Reply
  • Yes please. it tried to convert my to ts time series object. The function I am using requires univariate time series of class ts. I am using BFAST package for trend analysis.
    I created data frame from my date column and values. Then I used as.Date:
    NPP_t_date <- as.Date(NPP_t_date$date)
    # I dropped the first column to omit the name
    dataset_as_time_series <- ts(data = NPP_t_date[-1], start=2000, end=2015, frequency = 1)
    – first I tried to plot the time series and got this error (may be because I have 1415565 columns :
    Error in plotts(x = x, y = y, plot.type = plot.type, xy.labels = xy.labels, :
    cannot plot more than 10 series as "multiple"
    – I tried to run this function for gap fill NA —tsgf1 1 and only the first element will be used

    Reply

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.

Menu
Top