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

rolling function to get coeff of regression from a list of dataframes

I want to get coefficient of regression for each data frame in a list of dataframes with a rolling period but somehow I am getting very different result from what I am looking for. I have tried the following code: my data looks like this…
philomath
  • 23
  • 6
1
vote
0 answers

Rolling Beta regression for multiple stocks in R

I'm trying to calculate a rolling Beta regression for multiple stocks with a width of 12 past months. I have the following dataset Looks like: I was searching a lot of posts, but somehow I didn't get it to work for my data frame. func1 <- . %>%…
a.student
  • 11
  • 3
1
vote
1 answer

r - compute rolling sum by id within specific time frame

I want to calculate count of rows before current row in previous 1 year window by id. Here's my data: df <- structure(list(id = c("1", "1", "1", "1", "2", "2", "2", "2", "2", "2", "2", …
sm925
  • 2,648
  • 1
  • 16
  • 28
1
vote
2 answers

Calculating moving differences across columns per row in r

I would like to do calculations across columns in my data, by row. The calculations are "moving" in that I would like to know the difference between two numbers in column 1 and 2, then columns 3 and 4, and so on. I have looked at "loops" and…
Mary
  • 41
  • 5
1
vote
1 answer

Shifting of running mean output from the rollapply function in R

I am trying to plot the a time series with its corresponding 9 year running mean. I am using the rollapply function from the "zoo" package. I don't know why the "running mean" time series is not aligned properly even though I change the "align" in…
Lyndz
  • 347
  • 1
  • 13
  • 30
1
vote
3 answers

Dynamics "rollapply" in R

I am thinking of getting max of (N-1) rows by group in R (but not including the current row). Not sure how should I start with since each group has dynamics number of rows. For example: Thanks =)
skw1990
  • 63
  • 6
1
vote
3 answers

R - Insert a True every nth row based on previous rows

Test data Frame: a<-data.frame(True_False = c(T,F,F,F,F,T,F,T,T,T,F,F,F,F,F,F,F,F)) True_False 1 TRUE 2 FALSE 3 FALSE 4 FALSE 5 FALSE 6 TRUE 7 FALSE 8 TRUE 9 TRUE 10 TRUE 11 …
Silentdevildoll
  • 1,187
  • 1
  • 6
  • 12
1
vote
2 answers

How to find rolling sd over several months with daily data in R

I am fairly new to R and am trying to find a rolling standard deviation over a period of several months (3, 6, 9) in integer groups. For instance, for a year of data and three groups, I would like to find the standard deviation for each group 1, 2,…
csny
  • 13
  • 5
1
vote
1 answer

Building a rolling mean forecast

I have a series of intermittent demand called parts (sample below) and I want to develop a rolling mean forecast of a training set and a test set. My code is below as well. the series fitmean calculates the rolling mean, but there are two…
Angus
  • 355
  • 2
  • 12
1
vote
1 answer

Genome coverage as sliding window

I mapped my reads to my assembly using the bwa mem algorithm and extracted the number of reads per base (= coverage) using samtools depth. The resulting file is the following: 1091900001 1 236 1091900001 2 245 1091900001 3 265 1091900001 …
rororo
  • 815
  • 16
  • 31
1
vote
1 answer

R- rolling customized function dplyr

I need to extract outliers from a ts. I wanted to use a customized function like: es_outlier<-function(vect){ m=mean(vect) s=sd(vect) vector_final=abs(vect)>abs(m+s*1.5) return(vector_final ) } My table would be (a short…
GabyLP
  • 3,649
  • 7
  • 45
  • 66
1
vote
1 answer

rollapply mean with fill as smaller window when doesn't fit

Here's sample df: tt <- as.Date("2000-01-01") + c(1, 2, 5, 6, 7, 8, 10) z <- zoo(seq_along(tt), tt) ## - fill it out to a daily series, zm, using NAs ## using a zero width zoo series g on a grid g <- zoo(, seq(start(z), end(z), "day")) zm <-…
conv3d
  • 2,668
  • 6
  • 25
  • 45
1
vote
2 answers

Rolling Regression by Group

Hi I have a panel data set. I'd like to do a rolling window regression for each firm and extract the coefficient of the independent var. y is the dependent var and x is the independent var. Rolling window is 12. That is, the first regression uses…
Dylan
  • 51
  • 1
  • 4
1
vote
1 answer

Rolling changes of values and percentage

I'm helping a friend with some R homework for a apparently badly taught R class (because all the stuff covered in the class and the supplementary material doesn't help). We have two datasets. One contains daily discrete returns of a company share…
1
vote
1 answer

r- rollapply across a mutiple file database

I have a large database that I've split into multiple files. Each file is saved in the same directory, and there is a numerical sequence in the naming scheme so the order of the database is maintained. Ive done this to reduce the time and memory it…
Phil_T
  • 942
  • 9
  • 27