This package helps users who want to put data.frame or tbl_df objects into xts format easily. Xts is a powerful package used to convert data frames into time-series. This package is widely used in other packages in R too.
The problem is that often times users want to move from a tbl format, or a tidyverse format, into xts, but doing so can be an onerous task. This package aims to overcome this problem by bridging the tbl_df or data.table to xts gap. It also allows the user to use a spread_by argument for an easy character column xts conversion for tidy data.
To illustrate the ease with which tbl2xts transforms data.frames or tbl_dfs into xts format, consider the following examples.
Transform a dataframe into xts format:
Notice as of version 1.0.0 of this package, inputs are now quo_sures ~ so you don’t have to use quotations for parameters.
The input data frame requires a valid date column (similar to the requirement for the xts package). The function will check if a column date, DATE, or Date exists, or alternatively look for a valid timeBased object in your worfkile and rename it Date. This column can be either a Date, POSIXct, chron, yearmon, yearqtr or timeDate (see ?timeBased) column. The validity of the date column can be tested using:
If TRUE, it is a valid date column.
We could also add a suffix or prefix to the column name created. In the example below, adding spread_name_pos = “Suffix” makes columns
You could also easily transform multiple numeric columns into xts using the spread_by functionality:
Notice for this example, I forced a spread_name_pos = “Suffix” to allow for differentiation as you chose to spread_by and have multiple cols_to_xts.
If you set explicitly set spread_name_pos = “NONE”, you will be warned of the “Suffix” override.
You could also use a vector input for transformation:
tbl_xts also facilitates the use of tbl_df data frames in packages that use xts. As an illustration, see the output for TRI with the package PerformanceAnalytics. As we are working with Total Return Index values, suppose we wanted to calculate the weekly returns, and then the … using PerformanceAnalytics. This can now be achieved with neat code as follows:
Here I show how you can easily do quite advanced calculations and seemlessly move from tbl to xts back to tbl. Below I use xts’ apply.yearly and PerformanceAnalytics’ suite of calculations that allow us to do some nice analytics easily:
Note how easy we could do quite advanced calculations in a single pipe. E.g. if tasked with producing a plot for the annual CVaR numbers for our countries:
library(ggplot2) library(tidyr) TRI %>% tbl_xts(., cols_to_xts = TRI, spread_by = Country) %>% PerformanceAnalytics::Return.calculate(.) %>% xts::apply.yearly(., FUN = PerformanceAnalytics::CVaR) %>% xts_tbl %>% tidyr::gather(Country, CVaR, -date) %>% ggplot() + geom_line(aes(date, CVaR)) + theme_bw() + facet_wrap(~Country)