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

Problems with Rollapplyr function due to NA values (that should not be removed) in R

I have a dataframe: date comp ei 1 1/1/73 A NA 2 1/4/73 A 0.6 3 1/7/73 A 0.7 4 1/10/73 A 0.9 5 1/1/74 A 0.4 6 1/4/74 A 0.5 7 1/7/74 A 0.7 8 1/10/74 A 0.7 9 1/1/75 A 0.4 10 1/4/75 A 0.5 11 …
0
votes
1 answer

R - Rolling sum of two columns in data.table

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

filter out rolling mean results with limited data

I am trying to calculate the rolling mean of a time series. I have no problems with the calculation, however, looking at the results, there are locations along the time series where the rolling mean occurs based on one or two values surrounded by a…
johnnyg
  • 129
  • 8
0
votes
1 answer

Using rollapply to compute a week on week percentage change

I have some data which looks like: date col1 col2 col3 1 2020_09_01 53542. 22133. 25295. 2 2020_09_02 54157. 22505. 25327. 3 2020_09_03 54137. 23115. 24993. 4 2020_09_04 50795. 23127. 24166. 5…
user8959427
  • 2,027
  • 9
  • 20
0
votes
0 answers

Why does the rollapply function in R returns a matrix when applied to a vector?

I have a tibble of daily financial data. Similar to the below: head(df) tibble [2,871 x 2] (S3: tbl_df/tbl/data.frame) Price | Volatility 15.4 17.5 14.7 17.8 14.4 17.7 14.9 17.8 ... ... I have written a function to calculate…
0
votes
0 answers

How to parallelize rollapply?

I would like to parallelize the following rollapply in order to make it faster: Function for rollapply euclidiansum <- function(x) sum(((standardize(x[,"Passenger.x"]) - standardize(x[,"Passenger.y"]))^2)) Calculation tidyverse_rolling_euclidian…
0
votes
2 answers

Sum column values over a window and report the values of the previous window

I´m having a data.frame of the following form: ID Var1 1 1 1 1 1 3 1 4 1 1 1 0 2 2 2 2 2 6 2 7 2 8 2 0 3 0 3 2 3 1 3 3 3 2 3 4 and I would like to get there: ID Var1 X 1 1 0 1 …
0
votes
1 answer

rollapply with a normalisation function using multiple column data

I have some data that I would like to normalise using a rolling function. My objective is to make the three var1, var2 and var3 variables time series comparable between themselves. My current code only scales each variable individually based on its…
user113156
  • 6,761
  • 5
  • 35
  • 81
0
votes
1 answer

How to create a temporary vector inside a for loop in R?

I am trying to create a simple for loop in R that should create a temporary subset vector from the main data vector and do subsequent calculations on the temporary vector. The following is my code: count <- 0 window_size<-100 for (counter in…
jenifer
  • 3
  • 3
0
votes
1 answer

How to calculate rolling mean for multiple columns at once with a groupby and select in dplyr, while ignoring the groupby columns

I am trying to get rolling means for many columns at once, but I am running into difficulty because my grouping variables are not numeric. If I were to do a rolling mean for one column at a time, my code should look something like this…
0
votes
3 answers

rollapplyr() creates only one new column instead of multiple columns for each column in .SDcols

I am trying to use rollapplyr() on a data table an use it to generate various columns via .SDcols. Rather than putting the results for one column next to the results for another column, however, rollapplyr() stacks the results for each column below…
koteletje
  • 625
  • 6
  • 19
0
votes
1 answer

In R: how to conditionally use rollapply based on the number of NA values in the window?

Basic Problem: I'm new to R (and to programming in general) so I apologize if this post is not well formatted. I am currently using R to do some analysis on weather data. In short, I need to take the moving average of minimum temperatures for each…
0
votes
1 answer

How to calculate sd(x) using 1 hour rolling window when I have irregular time series data?

Suppose I have the following zoo object: prices.zoo <- read.zoo(data.frame(tm = c(as.POSIXct('2000-05-10 07:50:00'), as.POSIXct('2000-05-10 08:55:00'), as.POSIXct('2000-05-10 09:00:00'), as.POSIXct('2000-05-10 09:10:00'), as.POSIXct('2000-05-10…
Denis
  • 11,796
  • 16
  • 88
  • 150
0
votes
1 answer

Is there an R function for sumproduct the last 4 rows in a dataframe?

I need a function like sumproduct (excel) but only for the last 4 rows. Im using dplyr and i tried sumproduct with rollapply but i can only sum the last 4 rows of one column while i need multiply 2 columns by each row and then sum it by the last 4…
0
votes
1 answer

Problems with rollapply in regression equation

I am working with daily stock data and I am trying to compute the monthly betas in month t from daily stock data on t-11 month time window (e.g. the beta in Dec comprises daily stock data from Jan up to and including Dec). Additionally, I want to…
Mia
  • 13
  • 4