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

rollapply for cumulative window

I am trying to figure out how to use R rollapply on a data window which expands cumulatively from the beginning of data. At time t, roll apply should use data in the 1:t range. At time t+1, it should use 1:t+1, and so on. Thanks a lot John
john
  • 63
  • 7
0
votes
1 answer

Using rollapply() to find modal value

I've got panel data and have been playing around with k-means clustering. So now I've got a panel of factor values that are mostly stable but I'd like to smooth that out a bit more so that (for example) the data says "Wyoming was in group 1 in…
Rick_Weber
  • 26
  • 1
  • 4
0
votes
1 answer

Sum over rows (rollapply) with time decay

This is a follow on question to a question I posted earlier (see Sum over rows with multiple changing conditions R data.table for more details). I want to calculate how many times the 3 subjects have experienced an event in the last 5 years. So have…
Rkook
  • 63
  • 1
  • 11
0
votes
3 answers

How to calculate mean with expending window

I have a data frame below. I wondered how to calculate the mean for column 'value_t' by expanding window starting from '2014-1-5'. e.g. val(1)=mean(1:5), value(2)=mean(1:6), value(3)=mean(1:7). I hope the algorithm is efficient (w/o…
YYY
  • 605
  • 3
  • 8
  • 16
0
votes
0 answers

rollapply mean group by at defined interval

I have huge size data frame with several columns: 1) gridID 2) species code(SPCD) 3) related values(index) 4) related values(index1) and 5) latitude. but GridID is not unique numbers as GridID has multiple SPCD For example, tbl =…
kclick
  • 45
  • 8
0
votes
2 answers

Rollapply breaks and start again in R?

I have a data frame which looks like z<-data.frame(a=c(seq(1990,1995,1), 1997,1998,1999,2001,2002,2003), b=seq(90,101,1)) I use function rollapply(b, 3, sd, align='right') to calculate standard deviation. The thing I want to do is that function…
Jack
  • 167
  • 2
  • 12
0
votes
1 answer

Using rollapply on data set with 30 columns

I'm very new to R and are trying to get the means of gene expression data set. The files is a csv file and is has 185 rows (gene names) and 30 columns (time points in triplicates). I use the following commands to import the csv. file into…
0
votes
1 answer

R - rollapply with reverse window

I have a matrix (V) with daily stock returns between a certain time period on which I try to get the variance of the preceding 1-month (=21 days) and 6-months (=126 days). This is how I would do it based on future 1-month and 6-months…
0
votes
3 answers

R Sum complete cases of two columns

How can I sum the number of complete cases of two columns? With c equal to: a b [1,] NA NA [2,] 1 1 [3,] 1 1 [4,] NA 1 Applying something like rollapply(c, 2, function(x) sum(complete.cases(x)),fill=NA) I'd like to get back a single…
Travis Liew
  • 787
  • 1
  • 11
  • 34
-1
votes
1 answer

How can i check if a condition is satisfied with forward steps in different columns in R?

i have a data frame in R that looks like this : library(tidyverse) df = tibble(Date = seq(as.Date("2022/1/1"), by = "day", length.out = 5), y =…
Homer Jay Simpson
  • 1,043
  • 6
  • 19
-1
votes
1 answer

Obtaining trading days from rollapply in R

I have following simulated dataset of y column with fixed trading days (say 250) of 2018. data # A tibble: 249 × 2 Date y 1 2018-01-02 00:00:00 0.409 2 2018-01-03 00:00:00 -1.90 3 2018-01-04…
Homer Jay Simpson
  • 1,043
  • 6
  • 19
-1
votes
2 answers

How to compute rolling skewness for trailing 12months sliding window using Daily Returns in R?

I have Daily Returns for Stocks with a column containing Date in the format dd-mm-yy. I want to compute skewness for a month based on its trailing 12months Daily Returns using R. The Data looks like this For Instance: If there are Daily Returns of…
Aaryan
  • 31
  • 4
-1
votes
1 answer

rollapplyr on missing records related by date

How do I apply rollapplyr on the following data to allow it be sensitive to the date field? Because currently I am able to apply the rolling (blind to the date) over the dataset with eg. 4-quarters period and minimum of 2 observations in the 4…
yeeen
  • 4,911
  • 11
  • 52
  • 73
-1
votes
2 answers

Argument is not numeric or logical with function rollapply, followed by NAs introduced by coercion

I am trying to calculate means each 3 observations in my dataframe which is based on 10 minutes data and I am trying to average it down to half an hour. My data looks like this: Date Value 2017-09-20 09:19:59 96.510 2017-09-20…
-1
votes
1 answer

Average of last lowest N prior values by group

Basically I want to do a rolling average of the lowest N values. I have tried using mydata$lowest_n = rollapply(values, 5, mean(sort[1:5]), align=c('right')) But I cannot get this to work. again this needs to be rolling as this is a time…
runningbirds
  • 6,235
  • 13
  • 55
  • 94
1 2 3
17
18