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
2 answers

How can I rollapply or slide in a data frame in R with sliding width as well?

I have data frame in R that looks like this : a b 8 -16 19 -26 30 -36 41 -46 52 -56 I want to slide it or rollapply it with width 3 in both columns and calculate the the sum of the two minimum. BUT.!!!! I want progressively go…
Homer Jay Simpson
  • 1,043
  • 6
  • 19
1
vote
1 answer

How can I rollapply in R with two columns of a data frame?

I have a data frame that looks like this: library(tidyverse) a = seq(-5000,1500,length.out=31);a b = seq(2000,-5000,length.out=31);b w = tibble(a,b)%>%base::print(n=31) # A tibble: 31 × 2 a b 1 -5000 2000 2…
Homer Jay Simpson
  • 1,043
  • 6
  • 19
1
vote
2 answers

zoo::rollapply generates an error when applying a function from another package to a column of a dataframe

Here's a miminal example: df_1 <- data. Frame( a = 1:10) I have no trouble computing a rolling mean on a using zoo::rollapply zoo::rollapply(df_1$a, 5, "mean", fill = NA, align = "right") [1] NA NA NA NA 3 4 5 6 7 8 But if I try to do the…
Thomas Philips
  • 935
  • 2
  • 11
  • 22
1
vote
1 answer

Applying filter using rollapply in R

DT <- data.table( N = 1:16, x = c(11,11,11,11,11,11,11,11,21,21,21,21,21,21,21,21), y = rep(1:4,4,4,4,1:4,4,4,4), z = c(53,71,27,64,43,62,61,85,44,56,23,37,31,48,80,38), min = c(2.74,2.77,2.23,2.98,2.25,2.48,2.46,2.22,3.07, …
1
vote
2 answers

Multiple linear regression by group in a rolling window in R

My dataframe looks like this: Date = c(rep(as.Date(seq(15000,15012)),2)) Group = c(rep("a",13),rep("b",13)) y = c(seq(1,26,1)) x1 = c(seq(0.01,0.26,0.01)) x2 = c(seq(0.02,0.26*2,0.02)) df =…
Giselle J
  • 13
  • 3
1
vote
2 answers

Using rollmean filtering out NA with threshold

I am trying to apply a rollapply mean function to a dataframe with large chunks of missing data and single points interspersed throughout the missing data. Using my current form of rollapply, only one non-NaN value is needed and averaged to all…
johnnyg
  • 129
  • 8
1
vote
2 answers

Rollapply percentage from logical conditions (Rolling rate in R )

I have a data frame in R with two columns with logical conditions that looks like this : check1 = as.logical(c(rep("TRUE",3),rep("FALSE",2),rep("TRUE",3),rep("FALSE",2))) check2 = as.logical(c(rep("TRUE",5),rep("FALSE",2),rep("TRUE",3))) dat =…
Homer Jay Simpson
  • 1,043
  • 6
  • 19
1
vote
1 answer

rollapply for last reported values and not last reported time periods

I have some data which looks like: date ID var1 var2 var3 1 2005-02-22 5D0EAE -0.682 -0.682 -0.682 2 2005-04-29 5D0EAE 0.458 0.458 0.458 3 2005-06-28 80D368 0.178 0.0276 …
user113156
  • 6,761
  • 5
  • 35
  • 81
1
vote
1 answer

Using Rollapply to return both the Coefficient and RSquare

I have a dataset that looks something like this: data.table(x=c(11:30),y=rnorm(20)) I would like to calculate the rolling regression coefficient and rsquared over the last 10 items: dtset[,coefficient:=rollapply(1:20,width=10,FUN=function(a) { …
Kevin
  • 107
  • 5
1
vote
1 answer

Conditionally Rollmean based on another column value

I'm trying to take the moving average of a players fantasy points, based on their past performance against the same hand pitcher, over the past 3 games. FP <- data.frame(player = c(rep("A",10), rep("B",10), rep("C",10)), pitcher_hand…
1
vote
3 answers

rollapply for two-dimensional arrays (R)

There is something I want to do which I believe should be easily achieved with rollapply, but I am having some trouble. For simple vectors we have > a <- c(1:10) > a [1] 1 2 3 4 5 6 7 8 9 10 > rollapply(a, 2, mean) [1] 1.5 2.5 3.5 4.5 5.5…
Drubbels
  • 327
  • 2
  • 4
  • 11
1
vote
1 answer

Getting rolling average of multiple column by multiple condition, with dplyr and apply family

I'm performing an analysis on basketball data. This is how my dataset looks like (a really exemplified version of it): df<-data.frame(gmID = 1:20, H.Team = c("CLE", "MIA", "LAL", "PHI", "CLE", "DET", "CHI", "DAL", "UTA", "PHO",…
1
vote
1 answer

Variable Importance (rolling in 50days interval) for Gradient Boosting Model

I have the following data table dt.train, number of days and the function varImportance, to get the variable importance of a Linear Model: library(data.table) library(caret) library(xgboost) library(zoo) days <- 50 set.seed(123) dt.train <-…
MikiK
  • 398
  • 6
  • 19
1
vote
1 answer

variable lengths differ error when rollapply lm

I am trying to run a rolling window regression on a number of time series but encountered this strange problem. The following codes reproduce my data. I have a data frame containing returns named "rt" and a data frame containing factors named…
Billsyd
  • 25
  • 3
1
vote
1 answer

find the largest value change in every 6 hour in r

I have a dataframe with 2 column first: date-hour(every one hour one observation) and second column is temperature(th).I am trying to find What is the largest temperature change in my data series within a specific time (6 hours)? Consider both,…