tbl2xts v0.1.2


Monthly downloads



Convert Tibbles or Data Frames to Xts Easily

Facilitate the movement between data frames to 'xts'. Particularly useful when moving from 'tidyverse' to the widely used 'xts' package, which is the input format of choice to various other packages. It also allows the user to use a 'spread_by' argument for a character column 'xts' conversion.




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 example.

Example 1

Load the tbl_df data set included with the package, and transform it into xts format.

tbldata <- TRI
xtsdata <- tbldata %>% 
tbl_xts(., cols_to_xts = "TRI", spread_by = "Country", spread_name_pos = "Suffix")

Notice that the column name convention is to use the spread_by name, underscore, column name. In the example above, it is Country_TRI.

To now transform the xts object back into a tbl_df() object, simply use the inverse command:

xtsdata %>% xts_tbl()

Example 2

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:

tbldata <- TRI
tbldata %>% 
tbl_xts(., cols_to_xts = "TRI", spread_by = "Country") %>% 
lapply(.,Return.calculate, "discrete") %>% Reduce(merge,.) %>% table.DownsideRisk(.)

Note that lapply was used to apply the Return.Calculate to each column, while Reduce was used to merge all the lists created using lapply into a single data frame. We then pipe all the returns into, e.g., the table.DownsideRisk function, or any other applicable function in the PA package.


The input requires the data to be in data.frame() or tbl_df() form, with a valid date column being provided (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:

TRI[,"Date"] %>% .[[1]] %>% timeBased()

If TRUE, it is a valid date column.

Functions in tbl2xts

Name Description
xts_tbl xts_tbl
TRI This is a toy dataset, which is simply an example of a Total Return Index that can be used in packages requiring xts
tbl_xts tbl_xts
No Results!

Vignettes of tbl2xts

No Results!

Last month downloads


Date 2017-08-02
Type Package
License GPL-3
LazyData TRUE
RoxygenNote 6.0.1
Repository CRAN
NeedsCompilation yes
ByteCompile yes
VignetteBuilder knitr
Packaged 2017-08-02 13:44:29 UTC; nico.katzke
Date/Publication 2017-08-02 13:50:58 UTC

Include our badge in your README