Convert Time Series to Data Frame in R (Example)
In this tutorial, I’ll illustrate how to transform an xts time series object to a data frame in the R programming language.
The tutorial will contain the following:
Let’s get started.
Example Data & Add-On Packages
We first have to install and load the xts package to RStudio:
install.packages("xts") # Install & load xts package library("xts") |
install.packages("xts") # Install & load xts package library("xts")
The following time series data will be used as a basement for this tutorial:
my_ts <- xts(10:15, # Create time series object as.Date(c("2023-10-01", "2023-11-05", "2023-11-11", "2023-12-14", "2024-02-03", "2023-03-04"))) my_ts # Print time series # [,1] # 2023-03-04 15 # 2023-10-01 10 # 2023-11-05 11 # 2023-11-11 12 # 2023-12-14 13 # 2024-02-03 14 |
my_ts <- xts(10:15, # Create time series object as.Date(c("2023-10-01", "2023-11-05", "2023-11-11", "2023-12-14", "2024-02-03", "2023-03-04"))) my_ts # Print time series # [,1] # 2023-03-04 15 # 2023-10-01 10 # 2023-11-05 11 # 2023-11-11 12 # 2023-12-14 13 # 2024-02-03 14
As you can see based on the previous output of the RStudio console, our example time series data contains six rows at different dates.
Let’s check the data type of our time series object:
class(my_ts) # Check class of time series # [1] "xts" "zoo" |
class(my_ts) # Check class of time series # [1] "xts" "zoo"
As you can see, our data object has the xts & zoo classes.
Example: Convert Time Series to Data Frame Using as.data.frame() Function
This example demonstrates how to change the class of a time series object to the data.frame class.
For this task, we can apply the as.data.frame function as shown below:
data1 <- as.data.frame(my_ts) # Convert xts to data.frame data1 # Print data frame # V1 # 2023-03-04 15 # 2023-10-01 10 # 2023-11-05 11 # 2023-11-11 12 # 2023-12-14 13 # 2024-02-03 14 |
data1 <- as.data.frame(my_ts) # Convert xts to data.frame data1 # Print data frame # V1 # 2023-03-04 15 # 2023-10-01 10 # 2023-11-05 11 # 2023-11-11 12 # 2023-12-14 13 # 2024-02-03 14
As you can see based on the previous output, we have created a new data set called data1, which contains the values of our time series object as a column and the dates as row names.
Let’s check the class of our new data object:
class(data1) # Check class of data frame # [1] "data.frame" |
class(data1) # Check class of data frame # [1] "data.frame"
Our new data has the data.frame class. Looks good!
However, we may also convert the dates – that are currently the row names of our data frame – to an actual column.
Consider the R code below:
data2 <- data1 # Duplicate data frame data2$dates <- rownames(data2) # Convert row names to column rownames(data2) <- NULL # Reset row names data2 # Print new data frame # V1 dates # 1 15 2023-03-04 # 2 10 2023-10-01 # 3 11 2023-11-05 # 4 12 2023-11-11 # 5 13 2023-12-14 # 6 14 2024-02-03 |
data2 <- data1 # Duplicate data frame data2$dates <- rownames(data2) # Convert row names to column rownames(data2) <- NULL # Reset row names data2 # Print new data frame # V1 dates # 1 15 2023-03-04 # 2 10 2023-10-01 # 3 11 2023-11-05 # 4 12 2023-11-11 # 5 13 2023-12-14 # 6 14 2024-02-03
We have constructed another data frame object that contains the values and the dates of the input time series as separate variables.
Video & Further Resources
Would you like to know more about the transformation of an xts time series object to a data frame? Then you could have a look at the following video that I have published on my YouTube channel. In the video, I’m explaining the R syntax of this tutorial:
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, you may want to read some of the other posts on https://statisticsglobe.com/. I have released numerous tutorials on similar topics such as missing data, dates, graphics in R, and descriptive statistics.
- Draw Multiple Time Series in Same Plot
- Calculate Moving Average, Maximum, Median & Sum of Time Series
- Draw Time Series Plot with Events Using ggplot2 Package
- Convert Data Frame with Date Column to Time Series Object
- The R Programming Language
In summary: In this article you have learned how to convert a time series object to a data frame in the R programming language. If you have additional questions, tell me about it in the comments. Besides that, please subscribe to my email newsletter in order to receive updates on the newest tutorials.
Statistics Globe Newsletter