### Install RcppRoll Development Version Source: https://github.com/kevinushey/rcpproll/blob/master/README.md Install the latest development version of the RcppRoll package directly from GitHub. Ensure the devtools package is installed. ```r install_github("kevinushey/RcppRoll") ``` -------------------------------- ### Install RcppRoll Package Source: https://context7.com/kevinushey/rcpproll/llms.txt Install RcppRoll from CRAN or the development version from GitHub. ```r install.packages("RcppRoll") ``` ```r devtools::install_github("kevinushey/RcppRoll") ``` -------------------------------- ### Install RcppRoll from CRAN Source: https://github.com/kevinushey/rcpproll/blob/master/README.md Install the latest stable release of the RcppRoll package from the Comprehensive R Archive Network (CRAN). ```r install.packages("RcppRoll") ``` -------------------------------- ### Common Rolling Function Parameters Source: https://context7.com/kevinushey/rcpproll/llms.txt Demonstrates common parameters like window size, alignment, NA handling, fill values, subsampling, and custom weights for rolling functions. ```r library(RcppRoll) x <- c(1, 2, NA, 4, 5, 6, 7, 8, 9, 10) # Window size (n) roll_mean(x, n = 3) # 3-element window roll_mean(x, n = 5) # 5-element window ``` ```r # Alignment options roll_mean(x, n = 3, align = "center") # Default roll_mean(x, n = 3, align = "left") # Same as roll_meanl() roll_mean(x, n = 3, align = "right") # Same as roll_meanr() ``` ```r # Handle NA values roll_mean(x, n = 3, na.rm = TRUE) # Ignore NAs in calculation roll_mean(x, n = 3, na.rm = FALSE) # NAs propagate (default) ``` ```r # Fill values for edges (left, middle, right) roll_mean(x, n = 3, fill = NA) # Fill with NA roll_mean(x, n = 3, fill = 0) # Fill with 0 roll_mean(x, n = 3, fill = c(-1, 0, 1)) # Different fills roll_mean(x, n = 3, fill = numeric(0)) # No fill (shorter output) ``` ```r # Subsampling with 'by' parameter roll_mean(x, n = 3, by = 2) # Calculate every 2nd point roll_mean(x, n = 3, by = 3) # Calculate every 3rd point ``` ```r # Custom weights (overrides n) weights <- c(1, 2, 3) # Window size determined by weights length roll_mean(x, weights = weights) ``` ```r # Weight normalization roll_mean(x, weights = weights, normalize = TRUE) # Weights sum to n roll_mean(x, weights = weights, normalize = FALSE) # Use raw weights ``` -------------------------------- ### RcppRoll: Basic Rolling Minimum Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the rolling minimum over a sliding window. Useful for finding local minima. ```r library(RcppRoll) # Basic rolling minimum x <- c(5, 3, 8, 2, 9, 1, 7, 4, 6, 10) roll_min(x, n = 3) # [1] 3 2 2 1 1 1 4 4 ``` -------------------------------- ### RcppRoll: Basic Rolling Product Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the rolling product over a sliding window. Useful for compound returns or growth factors. ```r library(RcppRoll) # Basic rolling product x <- 1:10 roll_prod(x, n = 3) # [1] 6 24 60 120 210 336 504 720 ``` -------------------------------- ### Calculate Rolling Product Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the rolling product over a sliding window. Useful for calculating compound returns. ```r x <- c(1, 2, 3, 4, 5) roll_prod(x, n = 3) ``` ```r returns <- c(1.05, 0.98, 1.02, 1.01, 0.97, 1.03) roll_prod(returns, n = 3) # 3-period compound return ``` ```r roll_prodr(returns, n = 3) # [1] NA NA 1.050 1.010 1.000 1.010 ``` ```r roll_prodl(x, n = 2) # [1] 2 6 12 20 NA ``` -------------------------------- ### RcppRoll: Rolling Range Calculation Source: https://context7.com/kevinushey/rcpproll/llms.txt Compares rolling maximum and minimum to calculate the rolling range. ```r library(RcppRoll) # Compare with rolling minimum for range x <- rnorm(100) rolling_range <- roll_max(x, n = 5) - roll_min(x, n = 5) ``` -------------------------------- ### RcppRoll: Basic Rolling Sum Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the rolling sum over a sliding window. Efficient for cumulative totals. ```r library(RcppRoll) # Basic rolling sum x <- 1:10 roll_sum(x, n = 3) # [1] 6 9 12 15 18 21 24 27 ``` -------------------------------- ### RcppRoll: Basic Rolling Maximum Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the rolling maximum over a sliding window. Useful for finding local maxima. ```r library(RcppRoll) # Basic rolling maximum x <- c(5, 3, 8, 2, 9, 1, 7, 4, 6, 10) roll_max(x, n = 3) # [1] 8 8 9 9 9 7 7 10 ``` -------------------------------- ### RcppRoll: Rolling Minimum on Matrix Source: https://context7.com/kevinushey/rcpproll/llms.txt Applies the rolling minimum function to each column of a matrix. ```r library(RcppRoll) # Matrix input mat <- matrix(c(5, 3, 8, 2, 9, 1, 7, 4, 6, 10, 2, 6, 1, 8, 3, 9, 4, 7, 5, 10), nrow = 10) roll_min(mat, n = 3) # Applies to each column ``` -------------------------------- ### RcppRoll: Basic Rolling Median Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the rolling median over a sliding window. Useful for robust trend estimation. ```r library(RcppRoll) # Basic rolling median x <- c(1, 2, 10, 4, 5, 6, 100, 8, 9, 10) roll_median(x, n = 3) # [1] 2 4 5 5 6 8 9 9 ``` -------------------------------- ### RcppRoll: Basic Rolling Mean Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the rolling mean over a sliding window of size n. Center-aligned by default. ```r library(RcppRoll) # Basic rolling mean with window size 3 x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) roll_mean(x, n = 3) # [1] 2 3 4 5 6 7 8 9 ``` -------------------------------- ### RcppRoll: Rolling Median with NA Handling Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the rolling median, handling missing values with na.rm = TRUE. ```r library(RcppRoll) # Handle missing values y <- c(NA, 1, 2, 3, NA) roll_median(y, n = 3, na.rm = TRUE) # [1] 1.5 2.0 2.5 ``` -------------------------------- ### Calculate Rolling Standard Deviation Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the rolling standard deviation over a sliding window. Essential for measuring volatility. ```r library(RcppRoll) # Basic rolling standard deviation x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) roll_sd(x, n = 3) # [1] 1 1 1 1 1 1 1 1 ``` ```r # Financial volatility example prices <- c(100, 102, 99, 103, 98, 105, 101, 107, 100, 108) roll_sdr(prices, n = 5) # 5-day trailing volatility ``` ```r # Compare volatility across different window sizes vol_3 <- roll_sd(prices, n = 3) vol_5 <- roll_sd(prices, n = 5) ``` ```r # With normalization disabled roll_sd(x, n = 3, normalize = FALSE) ``` -------------------------------- ### RcppRoll: Left-Aligned Rolling Minimum Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the left-aligned rolling minimum (leading minimum). ```r library(RcppRoll) # Left-aligned for leading minimum x <- c(5, 3, 8, 2, 9, 1, 7, 4, 6, 10) roll_minl(x, n = 3) # [1] 3 2 2 1 1 1 4 4 NA NA ``` -------------------------------- ### RcppRoll: Right-Aligned Rolling Median Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the right-aligned rolling median over a sliding window. ```r library(RcppRoll) # Right-aligned rolling median x <- c(1, 2, 10, 4, 5, 6, 100, 8, 9, 10) roll_medianr(x, n = 3) # [1] NA NA 2 4 5 5 6 8 9 9 ``` -------------------------------- ### RcppRoll: Left-Aligned Rolling Median with Fill Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the left-aligned rolling median with custom fill values. ```r library(RcppRoll) # Left-aligned with fill x <- c(1, 2, 10, 4, 5, 6, 100, 8, 9, 10) roll_medianl(x, n = 5, fill = c(-1, 0, -1)) ``` -------------------------------- ### RcppRoll: Rolling Sum with Subsampling Source: https://context7.com/kevinushey/rcpproll/llms.txt Calculates the rolling sum at every nth point (subsampling). ```r library(RcppRoll) # Calculate at every nth point (subsampling) x <- 1:10 roll_sum(x, n = 3, by = 2) ``` -------------------------------- ### RcppRoll: Rolling Maximum with Fill Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the rolling maximum with fill values to maintain original length. ```r library(RcppRoll) # With fill to maintain original length x <- rnorm(100) roll_max(x, n = 5, fill = NA) ``` -------------------------------- ### RcppRoll: Rolling Mean on Matrix Source: https://context7.com/kevinushey/rcpproll/llms.txt Applies the rolling mean function to each column of a matrix. ```r library(RcppRoll) # Matrix input - applies rolling mean to each column mat <- matrix(1:20, nrow = 10, ncol = 2) roll_mean(mat, n = 3) ``` -------------------------------- ### RcppRoll: Right-Aligned Rolling Maximum Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the right-aligned rolling maximum (trailing maximum), common in finance. ```r library(RcppRoll) # Right-aligned for trailing maximum (common in finance) x <- c(5, 3, 8, 2, 9, 1, 7, 4, 6, 10) roll_maxr(x, n = 3) # [1] NA NA 8 8 9 9 9 7 7 10 ``` -------------------------------- ### RcppRoll: Right-Aligned Rolling Minimum Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the right-aligned rolling minimum (trailing minimum). ```r library(RcppRoll) # Right-aligned for trailing minimum x <- c(5, 3, 8, 2, 9, 1, 7, 4, 6, 10) roll_minr(x, n = 3) # [1] NA NA 3 2 2 1 1 1 4 4 ``` -------------------------------- ### RcppRoll: Rolling Sum with Custom Fill Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the rolling sum with custom fill values for left, middle, and right positions. ```r library(RcppRoll) # With custom fill values (left, middle, right) x <- 1:10 roll_sum(x, n = 3, fill = c(NA, 0, NA)) ``` -------------------------------- ### RcppRoll: Rolling Mean with Subsampling and Fill Source: https://context7.com/kevinushey/rcpproll/llms.txt Calculates rolling mean at every nth point with specified fill values. ```r library(RcppRoll) # Calculate every 2nd point with fill values x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) roll_mean(x, n = 3, by = 2, fill = NA) ``` -------------------------------- ### RcppRoll: Right-Aligned Rolling Mean Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the right-aligned rolling mean (trailing window) with NA fill. ```r library(RcppRoll) # Right-aligned rolling mean (trailing window) with NA fill x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) roll_meanr(x, n = 3) # [1] NA NA 2 3 4 5 6 7 8 9 ``` -------------------------------- ### RcppRoll: Weighted Rolling Sum Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes a weighted rolling sum using specified weights. ```r library(RcppRoll) # Weighted rolling sum weights <- c(0.2, 0.1, 0.1, 0.05, 0.05) roll_sum(1:25, n = length(weights), weights = weights) ``` -------------------------------- ### Calculate Rolling Variance Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the rolling variance over a sliding window. Useful for variance analysis and risk metrics. ```r library(RcppRoll) # Basic rolling variance x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) roll_var(x, n = 3) # [1] 1 1 1 1 1 1 1 1 ``` ```r # Right-aligned for trailing variance roll_varr(x, n = 3) # [1] NA NA 1 1 1 1 1 1 1 1 ``` ```r # Left-aligned variance roll_varl(x, n = 3) # [1] 1 1 1 1 1 1 1 1 NA NA ``` ```r # Matrix variance - calculates variance for each column mat <- matrix(rnorm(100), nrow = 50, ncol = 2) roll_var(mat, n = 10) ``` ```r # With different alignments and fill roll_var(x, n = 5, align = "right", fill = c(NA, 0, NA)) ``` -------------------------------- ### RcppRoll: Left-Aligned Rolling Mean Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the left-aligned rolling mean (leading window). ```r library(RcppRoll) # Left-aligned rolling mean (leading window) x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) roll_meanl(x, n = 3) # [1] 2 3 4 5 6 7 8 9 NA NA ``` -------------------------------- ### RcppRoll: Right-Aligned Rolling Sum Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes the right-aligned rolling sum (trailing sums). ```r library(RcppRoll) # Right-aligned for trailing sums x <- 1:10 roll_sumr(x, n = 3) # [1] NA NA 6 9 12 15 18 21 24 27 ``` -------------------------------- ### RcppRoll: Weighted Rolling Mean Source: https://context7.com/kevinushey/rcpproll/llms.txt Computes a weighted rolling mean, allowing more weight on recent values. ```r library(RcppRoll) # Weighted rolling mean weights <- c(1, 2, 3) # More weight on recent values x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) roll_mean(x, weights = weights) # Returns weighted average over 3-element windows ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.