Data Structure: XTS

Full Screen

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 an xts object from a numeric or matrix data with a time index.
  • as.xts() – Convert other objects (e.g., data frames, matrices) to xts.
  • zoo() – The zoo package is foundational for xts; use this to create an irregular time series object that can be converted to xts.
  • read.zoo() – Read data directly into a zoo object, which can then be converted to xts.

2. Index and Time Functions

  • index() – Get or set the index (time component) of an xts object.
  • time() – Get the time index of an xts object.
  • start() / end() – Get the start and end dates of an xts object.
  • periodicity() – Get the periodicity (e.g., daily, monthly) of an xts 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 an xts object.
  • head() / tail() – Display the first or last few rows of the data.

4. Time-Based Operations

  • merge() – Merge multiple xts objects, aligning on their time indices.
  • cbind() / rbind() – Combine xts objects by columns or rows.
  • lag() – Lag or lead an xts 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 the zoo package).
  • TTR::SMA() – Calculate a simple moving average (from the TTR 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 with NA values.
  • na.locf() – Carry the last observation forward to fill NA values.
  • na.approx() – Fill NA values by interpolation.

9. Time Zone and Format

  • tzone() – Get or set the time zone of an xts object.
  • format() – Format the index of an xts object to display as desired (e.g., year only, month only).

10. Plotting and Visualization

  • plot.xts() – Basic plotting function for xts objects.
  • dygraphs::dygraph() – An interactive time series plot (from the dygraphs package).
  • quantmod::chartSeries() – Plot financial time series data with technical indicators (from the quantmod package).

These functions and packages provide extensive functionality for handling, analyzing, and visualizing time series data in R with xts.