Data Structure: XTS
A list of common functions for working with xts
objects in R. The xts
(extensible time series) package is widely used for handling and analyzing time series data.
1. Creating and Converting to xts
xts()
– Create anxts
object from a numeric or matrix data with a time index.as.xts()
– Convert other objects (e.g., data frames, matrices) toxts
.zoo()
– Thezoo
package is foundational forxts
; use this to create an irregular time series object that can be converted toxts
.read.zoo()
– Read data directly into azoo
object, which can then be converted toxts
.
2. Index and Time Functions
index()
– Get or set the index (time component) of anxts
object.time()
– Get the time index of anxts
object.start()
/end()
– Get the start and end dates of anxts
object.periodicity()
– Get the periodicity (e.g., daily, monthly) of anxts
object.
3. Subsetting and Accessing Data
[]
– Use bracket notation to subset by rows (time index) and/or columns.first()
– Extract the first few observations based on a specified period (e.g.,first(data, "1 month")
).last()
– Extract the last few observations based on a specified period.window()
– Extract a specific range of dates from anxts
object.head()
/tail()
– Display the first or last few rows of the data.
4. Time-Based Operations
merge()
– Merge multiplexts
objects, aligning on their time indices.cbind()
/rbind()
– Combinexts
objects by columns or rows.lag()
– Lag or lead anxts
series by a specified number of time steps.diff()
– Calculate the difference between consecutive observations.apply.daily()
,apply.weekly()
,apply.monthly()
, etc. – Apply a function to data grouped by specific time periods.
5. Aggregation and Resampling
to.daily()
,to.weekly()
,to.monthly()
, etc. – Convert high-frequency data to a lower frequency (e.g., daily to weekly).apply.yearly()
,apply.quarterly()
, etc. – Aggregate data based on a specified period by applying a function.period.apply()
– Apply a function over specified time periods.
6. Transformation and Calculations
cummax()
,cummin()
– Cumulative maximum and minimum.cumprod()
– Cumulative product.cumsum()
– Cumulative sum.na.locf()
– Fill missing values with the last observation carried forward.na.approx()
– Fill missing values using linear interpolation.
7. Rolling and Moving Functions
rollapply()
– Apply a function over a rolling window (from thezoo
package).TTR::SMA()
– Calculate a simple moving average (from theTTR
package).TTR::EMA()
– Calculate an exponential moving average.TTR::runMax()
,TTR::runMin()
, etc. – Calculate rolling maximum, minimum, and other statistics.
8. Handling Missing Data
na.omit()
– Remove rows withNA
values.na.locf()
– Carry the last observation forward to fillNA
values.na.approx()
– FillNA
values by interpolation.
9. Time Zone and Format
tzone()
– Get or set the time zone of anxts
object.format()
– Format the index of anxts
object to display as desired (e.g., year only, month only).
10. Plotting and Visualization
plot.xts()
– Basic plotting function forxts
objects.dygraphs::dygraph()
– An interactive time series plot (from thedygraphs
package).quantmod::chartSeries()
– Plot financial time series data with technical indicators (from thequantmod
package).
These functions and packages provide extensive functionality for handling, analyzing, and visualizing time series data in R with xts
.