r - Rollmean with element value dependant window? -


i'm looking fast way following:

there factor f m levels , length n. n huge (millions). m thousands. want calculate each element x in f rolling frequency of x's level within f.

for example if have 2 levels , factor [1,2,2,2,1,1,2,1,2,1,1,1,2] rolling window 3 result supposed [1/3, 1/3, 2/3, 1, 1/3, 2/3, 1/3, 2/3, 2/3, 2/3, 2/3, 1, 1/3]

that done zoo::rollmean (pseudocode):

res <- rep(na, n) foreach level in {m} { ids <- (f == m) res[ids] <- rollmean(f == m, 3, fill = 0, align="right") [ids] # running rollmean on logical vector. first 3 elem wrong did't find suitable fill parameter. } 

edit: if run pseudocode above you'll wrong result (first 2 values corrupted) didn't find right fill= param rollmean() make work first 2 elements , window 3 assumed.

but damn slow, bcs logical vector sparse (most values false).

what generate seq(1, n) (list of indexes) , split tapply() f's levels. give me list of indexes each level. great go on each list , each index there find if there indexes less greater (index - window). , count them.

i have tried user function under lapply(), it's slow too.

in other words shall rollmean() ordered vectors decides element belongs window not position, value. or dynamically changes window size based on vector values (same other point of view).

is there in r that? or other faster method that?

this package might of problem:

library("devtools") install_github("boringtrees","mgahan")  # bring in library library("boringtrees") 

here similar example described. note can change number of cores if need more speed. should pretty fast because utilizes speed of data.table package.

set.seed(1) trans_dates <- as.date(c(31,33,65,96,150,187,210,212,240,273,293,320,                          32,34,66,97,151,188,211,213,241,274,294,321,                          33,35,67,98,152,189,212,214,242,275,295,322),origin="2010-01-01") cust_id <- c(rep(1,12),rep(2,12),rep(3,12)) target <- rpois(36,3) require("data.table") data <- data.table(trans_dates,cust_id,target)  data[,roll:=rollingbycalcs(data=data,bylist="cust_id",dates="trans_dates",         target="target",lower=0,upper=31,incbounds=t,stat=mean,na.rm=t,cores=1)] 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -