Convert xts Time Series Object to Data Frame in R (Example Code)

In this R tutorial you’ll learn how to transform an xts time series object to the data frame class.

Setting up the Example

install.packages("xts")                     # Install & load xts
library("xts")
time_series <- xts(1:5,                     # Construct time series object
                   as.Date(c("2022-11-08",
                             "2021-05-14",
                             "2020-07-18",
                             "2024-12-12",
                             "2022-03-24")))
time_series
#            [,1]
# 2020-07-18    3
# 2021-05-14    2
# 2022-03-24    5
# 2022-11-08    1
# 2024-12-12    4

Example: Transforming xts Object to Data Frame Using as.data.frame() Function

data_frame <- as.data.frame(time_series)    # Convert time series to data frame
data_frame
#            V1
# 2020-07-18  3
# 2021-05-14  2
# 2022-03-24  5
# 2022-11-08  1
# 2024-12-12  4

Related Articles

Have a look at the following list of R programming tutorials. They discuss topics such as character strings, factors, and time objects.

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