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
3
votes
1 answer

rollapply with zoo and sub-daily data

I have a dataset with unequally spaced observations and frequently observations occur more than once per day. I'd like to apply a function to windows of my data, but I want the windows to be defined by time rather than by row. For example, I'd…
random_forest_fanatic
  • 1,232
  • 1
  • 12
  • 30
3
votes
1 answer

apply function to rolling window in panel data in R

I'm trying to apply a function (say standard deviation) in a rolling window, by category: I have the following data: cat = c("A", "A", "A", "A", "B", "B", "B", "B") year = c(1990, 1991, 1992, 1993, 1990, 1991, 1992, 1993) value = c(2, 3, 5, 6, 8,…
ec0n0micus
  • 1,075
  • 2
  • 12
  • 19
3
votes
3 answers

rollapply with a function taking a matrix returns "incorrect number of dimensions"

I designed my own function called SharpeRatio(data) Where data is an nx2 matrix. The function works fine for a given matrix dat, however when I try to use rollapply(dat, 20, SharpeRatio) I get the following error: Error in dat[, 1] : incorrect…
msmf14
  • 1,449
  • 4
  • 13
  • 19
3
votes
2 answers

Sum pairs of columns by group

I wish to sum pairs of columns by group. In the example below I wish to sum pairs (v1 and v2), (v3 and v4), and (v5 and v6), each by r1, r2 and r3. I can do this using the sapply statement below and I get the correct answer. However, the required…
Mark Miller
  • 12,483
  • 23
  • 78
  • 132
2
votes
1 answer

Applying a statistical test on sliding window

Applying functions on rolling windows of zoo objects is normally quite straightfoward, e.g. a moving average: z <- zoo(1:10, as.Date(31:40)) rollapply(z, 4, mean, align="right") Now I want to do the same thing with a statistical test, i.e. apply a…
vonjd
  • 4,202
  • 3
  • 44
  • 68
2
votes
1 answer

rolling summarize conditioned to multiple variables on another dataframe

I have the following data example: trap_data <- structure(list(site = c(1, 2, 3, 3), trap_date = structure(c(18809, 18809, 18307, 18322), class = "Date")), class = "data.frame", row.names = c(NA, -4L)) climate <- structure(list(site = c(1, 1, 1,…
Wilson Souza
  • 830
  • 4
  • 12
2
votes
1 answer

Replace NA's in R with the current rollapply value

I have a 10 year dataset from Tesla returns (2 day difference percentage) tsla <- quantmod::getSymbols("TSLA", from = base::as.Date("2011-01-01"), to = base::as.Date("2022-01-31"), auto.assign = F) tsla = as_tibble(tsla) head(tsla) Also I have a…
Homer Jay Simpson
  • 1,043
  • 6
  • 19
2
votes
2 answers

Rollapply in R time frame indexing

Let's say I have the following data frame of series. library(zoo) n=10 date = as.Date(1:n);date y = rnorm(10);y dat = data.frame(date,y) dat date y 1 1970-01-02 -0.02052313 2 1970-01-03 0.28255304 3 1970-01-04 -0.10718621 4 …
Homer Jay Simpson
  • 1,043
  • 6
  • 19
2
votes
2 answers

How to flag the values of a column based on values of another column in R by using rollapply?

I have a data frame, like this: df <- data.frame (T = c(1:20), L = c(1,2,9,4,6,8,3,4,2,5,2,5,9,10,3,5,1,1,2,2)) I want to Flag a T value Ti (and also Ti−1 and Ti+1) if Li is bigger than 6 (i.e. 3 values in total are flagged then). How to do it by…
2
votes
1 answer

Rolling stepwise regression with dplyr

I want to make an rolling stepwise regression with dplyr, do() and rollapply(). My code for the data looks like this: FUND_DATA <- tibble( DATE = 1:10, FUND1 = rnorm(10), FUND2 = rnorm(10), FUND3 = rnorm(10), FUND4 = rnorm(10)) These…
MeT
  • 21
  • 3
2
votes
1 answer

Assigning the numbers and summarising the number of counts in a sliding window in R

I have a df that looks like this: df <- (c( "P", "S", "E", "G", "R", "Q", "P", "S", "P", "S", "P", "S", "P", "T", "E", "R", "A", "P", "A", "S", "E", "E", "E", "F", "Q", "F", "L", "R", "C", "Q", "Q", "C", "Q", "A", "E", "A", "K", "C", "P", "K",…
student24
  • 252
  • 1
  • 9
2
votes
1 answer

how to make a rolling sum (or rolling average) with multiple variables

I'm new using the zoo package, so maybe it's an easy question. I have the following data frame (df): library(lubridate) library(zoo) library(dplyr) Date <- c("2010-01-28", "2010-01-28", "2010-02-28", "2010-02-28", "2010-03-28",…
Jones
  • 333
  • 1
  • 11
2
votes
1 answer

Combining rollapply() and weighted.mean() in a data.table apply() for multiple columns

I am trying to compute weighted averages for various columns in data.table with rollapply() and weighted.mean() as follows: DT <- data.table(id = rep(c(1,2), each = 5), var1 = 1:10, var2 = 11:20) col_names <-…
koteletje
  • 625
  • 6
  • 19
2
votes
2 answers

Techniques to Speed up rollapply with Custom Function (r)

I have a rollapply function that does something very simple, however over million data points this simple function is quite slow. I would like to know if it is possible to provide information to rollapply for how to make the next transition rather…
channon
  • 362
  • 3
  • 16
2
votes
1 answer

weighted rolling median in data.table (r)

I know tons of functions calculate the rolling median, but I could not find anything that calculates the weighted rolling median (I found ema, but that's average). Here is what I have tried *** edited on Jan 31 2019: I have found the code works fine…
1 2
3
17 18