tsibble is an R package for structured time-series analysis, offering an alternative to xts with a tidy data format and more intuitive time-based operations. Unlike xts, which stores time indices as row names, tsibble treats time as an explicit column, making it easier to manipulate and integrate with the tidyverse. It simplifies indexing, aggregation, and frequency transformations, automatically handling irregular timestamps and allowing seamless grouping across different time intervals.
Prerequisites
We load daily dollar exchange rates of the yen and the euro:
date eur yen
6640 2025-06-13 1.1552 144.09
6641 2025-06-16 1.1561 144.72
6642 2025-06-17 1.1479 145.25
6643 2025-06-18 1.1479 145.10
6644 2025-06-19 1.1494 145.45
6645 2025-06-20 NA NA
We need to convert the date variable (which is now a string) to a date:
xrates$date <-as.Date(xrates$date)tail(xrates)
date eur yen
6640 2025-06-13 1.1552 144.09
6641 2025-06-16 1.1561 144.72
6642 2025-06-17 1.1479 145.25
6643 2025-06-18 1.1479 145.10
6644 2025-06-19 1.1494 145.45
6645 2025-06-20 NA NA
Converting to a Time-Based Tsibble
We can now convert it to a tsibble:
xr <-as_tsibble(xrates, index = date)print(tail(xr))
# A tsibble: 6 x 3 [1D]
date eur yen
<date> <dbl> <dbl>
1 2025-06-13 1.16 144.
2 2025-06-16 1.16 145.
3 2025-06-17 1.15 145.
4 2025-06-18 1.15 145.
5 2025-06-19 1.15 145.
6 2025-06-20 NA NA
Aggregating at Different Frequencies
In the examples below, we compute for each period:
start_date: the first timestamp in the period
end_date: the last timestamp in the period
avg_*: the period average of each series
first_*: the value at the first timestamp
last_*: the value at the last timestamp
Monthly Aggregation
# convert daily tsibble to monthly summariesxr_monthly_methods <- xr %>%index_by(quarter =yearquarter(date)) %>%summarise(# First and last date in each quarterstart_date =first(date),end_date =last(date),# Numerical summaries for each currencyavg_yen =mean(yen, na.rm =TRUE),first_yen =first(yen),last_yen =last(yen),avg_eur =mean(eur, na.rm =TRUE),first_eur =first(eur),last_eur =last(eur) ) # preview the tailtail(xr_monthly_methods)
# build quarterly tsibble directly from the raw xrates dataxr_quarterly_methods <- xr %>%index_by(quarter =yearquarter(date)) %>%summarise(# First and last date in each quarterstart_date =first(date),end_date =last(date),# Numerical summaries for each currencyavg_yen =mean(yen, na.rm =TRUE),first_yen =first(yen),last_yen =last(yen),avg_eur =mean(eur, na.rm =TRUE),first_eur =first(eur),last_eur =last(eur) ) # preview the tailtail(xr_quarterly_methods)
xr_annual_methods <- xr %>%index_by(year =year(date)) %>%summarise(# First and last date in each quarterstart_date =first(date),end_date =last(date),# Numerical summaries for each currencyavg_yen =mean(yen, na.rm =TRUE),first_yen =first(yen),last_yen =last(yen),avg_eur =mean(eur, na.rm =TRUE),first_eur =first(eur),last_eur =last(eur) ) tail(xr_annual_methods)
In tsibble, the standard lag() (and dplyr::lag()) always shifts by rows, not by actual calendar intervals—even if your data are daily, any implicit gaps (e.g. weekends, holidays, or just missing dates) aren’t accounted for. To get a true “lag by 2 calendar days,” you use fill_gaps() to turn every implicit missing date into an explicit NA, so that one row = one calendar day. After that, lag(..., 2) really means “two days ago.”
# Fill the calendar gaps, then lag by rowsxr<-xr %>%fill_gaps() %>%# ensure every calendar day is presentmutate(eur_lag2 = dplyr::lag(eur, 2) # now lags EUR by 2 calendar days )tail(xr)
# A tsibble: 6 x 4 [1D]
date eur yen eur_lag2
<date> <dbl> <dbl> <dbl>
1 2025-06-15 NA NA 1.16
2 2025-06-16 1.16 145. NA
3 2025-06-17 1.15 145. NA
4 2025-06-18 1.15 145. 1.16
5 2025-06-19 1.15 145. 1.15
6 2025-06-20 NA NA 1.15
If the original data skipped a date, lag2 on that date will be NA rather than pulling the last available value.
Is Tsibble Easier?
tsibble simplifies time-series operations by eliminating the need for manual date conversion, making frequency aggregation more intuitive, and automatically detecting missing timestamps. Unlike other time-series packages that require additional steps for handling irregular data, tsibble provides built-in tools to manage gaps and ensure accurate time alignment, making it a more efficient and user-friendly choice for working with time-based data. However, for high-frequency financial data, other packages like xts might still be preferred.