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
1
vote
1 answer

Rollapply or similar for multi-condition thresholds in time series data

I am trying to find dates of yearly instances exceeding multi-condition thresholds in time series data. I used the rollapply function to identify the first date each year (lets call this date A) when the daily temperature exceeds B (B >= 5.0;…
b.coleman
  • 11
  • 2
1
vote
2 answers

R - How to make a mean/average of n previous values, excluding current observation (rolling average)

Could someone kindly advise how best to approach making a new column in a dataframe, where each observation is an average/mean of the previous 12 observations (excluding the current observation). I have failed so far to find a similar answer on here…
NW320d
  • 15
  • 4
1
vote
1 answer

Using Rollapply in a data table with a variable width in R

I want to use rollapply to a calculate a moving average in a data table in R. Each row in the data table is one month and I would like to average the last 36 months but will go as low as 24 if that is all that is available. The code below…
Kevin
  • 107
  • 5
1
vote
1 answer

Error when combining rollapply() and weighted.mean() in an lapply() data.table setting

I ran the following code: id <- c(67, 39, 39, 39, 39, 39, 39, 39, 58, 58, 58, 58, 58, 58) ratio <- c(0.5421248, 0.1558647, 0.1314578, 0.1095102, 0.1149908, 0.1645262, 0.1431160, 0.1633623, 1.1375268, 1.3219208, 1.3830684, 1.5942101,…
koteletje
  • 625
  • 6
  • 19
1
vote
3 answers

Result-feeding rolling window or rollapply with cumsum

Suppose I have the following zoo object: x.orig <- read.zoo(data.frame(date=seq(as.Date('2020-01-01'), as.Date('2020-01-10'), 1), v=c(1,2,3,100,4,5,1000,8,8,10))) 2020-01-01 2020-01-02 2020-01-03 2020-01-04 2020-01-05 2020-01-06 2020-01-07…
Denis
  • 11,796
  • 16
  • 88
  • 150
1
vote
1 answer

Pass the `width` parameter of `zoo:rollapply` as argument to the called function

How can I pass the width argument of the zoo:rollapply function to the FUN function called within rollapply? I put the values and widths as columns of a same data.frame: library(zoo) a = data.frame(v = 1:10, w = c(2,1,3,5,2,7,3,2,1,3)) # v w # 1…
ztl
  • 2,512
  • 1
  • 26
  • 40
1
vote
0 answers

Error when trying to add dates to a plot for rollapply correlation function

I have been trying to put dates from an single column in an excel sheet on the x-axis of a plot for a rollapply correlation function. cor.fun = function(x){cor(x)[1,2]} gdp.g <- cbind(economic_data$Global,economic_data$GDP) gdp.g.corr <-…
brenner
  • 11
  • 2
1
vote
2 answers

How do I call a function using a specific time window?

Suppose I have a zoo object (or it could be a data.frame) that has an index on "time of day" and has some value (see sample data below): val ... 2006-08-01 12:00 23 2006-08-01 12:01 24 2006-08-01 12:02 25 2006-08-01…
Denis
  • 11,796
  • 16
  • 88
  • 150
1
vote
3 answers

rollapply how to "ignore" certain observations and use variable width

I am trying to calculate mean for some data along a non-regular date sequence. For example, I have minute level data for specific periods of time during the day and I am interested in calculating 5 minute averages. However, I am not sure how does…
rjss
  • 935
  • 10
  • 23
1
vote
1 answer

Creating new factors using rollapply

I am trying to create a new factor in my dataset using rollapply. I want to create a factor that takes the mean of observations with a different month than the current month, for the previous 240 months (20 years). For this I require atleast the…
Bart
  • 317
  • 4
  • 18
1
vote
1 answer

sum of positive events over a 12 month rolling window

I am trying to count the number of positive events over a 12 month rolling window. I can create 365 rows of missing data per year and use zoo::rollapply to sum the number of events per 365 rows of data, but my data frame is really big and I want to…
lokes
  • 75
  • 8
1
vote
2 answers

How to calculate moving average by specified grouping and deal with NAs

I have a data.table which needs a moving average to be calculated on the previous n days of data (let's use n=2 for simplicity, not incl. current day) for a specified grouping (ID1, ID2). The moving average should attempt to include the last 2 days…
1
vote
1 answer

How to apply a function per row of a column in a data table with other rows as input?

For each row of column "Response", I would like to check if the 5 rows below it have "Response"-values (i.e. have no NAs) and if so, then I would like to calculate the mean and standard deviation of those 5 rows below. If any row, in those 5 rows…
Mary
  • 41
  • 5
1
vote
1 answer

rollapply based on values

I would like to resample a large data set with an unequal number of observations across the range in the data so that each range has an equal number of observations. It seems like rollapply would be the way to do this, but it doesn't appear that it…
TBP
  • 697
  • 6
  • 16
1
vote
3 answers

Rolling sum in R

df <- data.frame(x = seq(1:10)) I want this: df$y <- c(1, 2, 3, 4, 5, 15, 20 , 25, 30, 35) i.e. each y is the sum of previous five x values. This implies the first five y will be same as x What I get is this: df$y1 <- c(df$x[1:4],…
89_Simple
  • 3,393
  • 3
  • 39
  • 94