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 anxtsobject from a numeric or matrix data with a time index.as.xts()– Convert other objects (e.g., data frames, matrices) toxts.zoo()– Thezoopackage is foundational forxts; use this to create an irregular time series object that can be converted toxts.read.zoo()– Read data directly into azooobject, which can then be converted toxts.
2. Index and Time Functions
index()– Get or set the index (time component) of anxtsobject.time()– Get the time index of anxtsobject.start()/end()– Get the start and end dates of anxtsobject.periodicity()– Get the periodicity (e.g., daily, monthly) of anxtsobject.
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 anxtsobject.head()/tail()– Display the first or last few rows of the data.
4. Time-Based Operations
merge()– Merge multiplextsobjects, aligning on their time indices.cbind()/rbind()– Combinextsobjects by columns or rows.lag()– Lag or lead anxtsseries 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 thezoopackage).TTR::SMA()– Calculate a simple moving average (from theTTRpackage).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 withNAvalues.na.locf()– Carry the last observation forward to fillNAvalues.na.approx()– FillNAvalues by interpolation.
9. Time Zone and Format
tzone()– Get or set the time zone of anxtsobject.format()– Format the index of anxtsobject to display as desired (e.g., year only, month only).
10. Plotting and Visualization
plot.xts()– Basic plotting function forxtsobjects.dygraphs::dygraph()– An interactive time series plot (from thedygraphspackage).quantmod::chartSeries()– Plot financial time series data with technical indicators (from thequantmodpackage).
These functions and packages provide extensive functionality for handling, analyzing, and visualizing time series data in R with xts.