Questions tagged [rollapply]

rollapply is a function in the zoo package used to perform rolling operations on an object.

rollapply is a function in the zoo package used to apply operations sequentially over elements of an object. Examples are returning a rolling mean for a vector or performing a regression on each of several partially-overlapping subsets, or rolling subsets, of a data set.

256 questions
4
votes
3 answers

Efficient way to perform running total in the last 365 day window

This is what my data frame looks like: library(data.table) df <- fread(' Name EventType Date SalesAmount RunningTotal Runningtotal(prior365Days) John Email 1/1/2014 0 0 0 …
gibbz00
  • 1,947
  • 1
  • 19
  • 31
3
votes
4 answers

Apply rolling function which generates a list of data.frames (or single rbinded data.frame)

The two most commonly used functions for rolling functions (that I'm aware of) are zoo::rollapply and data.table::frollapply(). However, neither seems capable of running a function which generates a data.frame for each step and then returning them…
Wasabi
  • 2,879
  • 3
  • 26
  • 48
3
votes
2 answers

How can I find the most common sequences in my data using R?

I'm trying to figure out how I can use the rollapply function (from the Zoo package) to find sequences of most common strings within a dataset, but I also need to do group certain variables (e.g. date, row, etc.) Before I go any further, it's worth…
Japes
  • 209
  • 1
  • 10
3
votes
2 answers

Rolling a function on two columns in data.table

I have a data.table as follows - library(data.table) dt = data.table( date = seq(as.Date("2015-12-01"), as.Date("2015-12-10"), by="days"), v1 = seq(1, 10), v2 = c(5, rep(NA, 9)) ) dt date v1 v2 1: 2015-12-01 1 5 2: 2015-12-02 2…
Saurabh
  • 1,566
  • 10
  • 23
3
votes
2 answers

Rolling mean with changing window size on a large dataset

I want to compute the rolling mean over a vector whereby the window grows with each entry in the vector. Basically, I want to have the mean of all elements up to the i-th, i+1-th, i+2-th, and so forth. To make it more clear, I'll provide an example…
apitsch
  • 1,532
  • 14
  • 31
3
votes
0 answers

How to calculate rolling quantile for each day on intra day data with data.table

I would like to calculate a rolling quantile using data table, which contains data on several groups, for each group i have multiple days and within each day I have multiple observations. I do not want to calculate the rolling quantile for every…
ira
  • 2,542
  • 2
  • 22
  • 36
3
votes
0 answers

width parameter as variable in rollapply

I have a dataframe like this: df <- data.frame(width = c(1,2,2,4), values = c(10,11,13,20)) I want to use rollapply to compound the 'values' column, but I want to use a variable width for each row, getting the width from the appropriate row in the…
sotiris
  • 63
  • 1
  • 1
  • 8
3
votes
1 answer

Rolling prediction in a data frame using dplyr and rollapply

My first question here :) My goal is: Given a data frame with predictors (each column a predictor / rows observations) fit a regression using lm and then predict the value using the last observation using a rolling window. The data frame looks…
Jose Filho
  • 61
  • 3
3
votes
2 answers

Conditional extract from vector using rollapply

I am trying to extract a series of values in a vector that meet a certain condition. To illustrate this imagine I have the following vector: a <- c(1,2,1,3,12,3,2,15,1,1,1,1,4,5,20) I would like to isolate consecutive values that whose sum is less…
DHenry
  • 83
  • 5
3
votes
0 answers

Rolling Expected Shortfall and Tail Risk using PerformanceAnalytics

Since rolling VaR gives error when complete window is missing, I have done the following to estimate 36 months rolling VaR (36 months window missing for HAM5 and…
Jairaj Gupta
  • 347
  • 4
  • 16
3
votes
2 answers

Rolling lagged differences

Ok so I am looking to create rolling lagged differences in R. vec <- c(43.79979, 44.04865, 44.17308, 44.54638, 44.79524, 44.79524, 44.79524, 44.42195, 44.54638, 44.79524, 44.42195, 43.30206, 43.30206, 43.17764, 43.30206) > length(vec) [1] 15 This…
Andrew Bannerman
  • 1,235
  • 2
  • 16
  • 36
3
votes
1 answer

Calculate mean using rollapply only if certain percent of data is available

I have a column of hourly data and want to use rollapply to calculate the 24-hour rolling average for every hour. My data contains NA's and I only want to calculate the rolling average if 75% of the data for one 24-hour period is available,…
MaddieS
  • 71
  • 7
3
votes
4 answers

How to calculate the average slope within a moving window in R

My dataset contains 2 variables y and t [05s]. y was measured every 05 seconds. I am trying to calculate the average slope within a moving 20-second-window, i.e. after calculating the first 20-second slope value the window moves forward one time…
Eva
  • 33
  • 1
  • 4
3
votes
1 answer

Roll apply for partial time series in R

Given z <- zoo(c(1:10)) I want to be be able to aggregate to the following: > z 4 8 10 10 26 19 I have tried the following using rollapply but to no avail: > rollapply(zoo(c(1:10)), width = 4, FUN = "sum", by = 4, partial = TRUE, align =…
bob
  • 81
  • 1
  • 4
3
votes
4 answers

Using apply.rolling function over multiple columns

In zoo package, while using rollapply function, we get an logical option 'by.column = TRUE/FALSE'. If TRUE, FUN is applied to each column separately. However, in apply.rolling function of PerformanceAnalytics package I don't see any such option.…
Jairaj Gupta
  • 347
  • 4
  • 16
1
2
3
17 18