I'm not sure I understand what your filter intends to do, but could this not
be done more efficiently with logicals and the which? You also might need
the cumsum() function and diff() with the optional lag argument if I've
misunderstood your filter.
Specifically try this:
res = c(x,rep(NA,wait)) # make a copy to work on and include the extra NA
which we might turn into zeros, but will drop later
for (i in 1:wait) {res[which(x == 1) + i] <- 0}
res = res[1:length(x)] # drop the extra added length.
Michael Weylandt
On Fri, Jul 29, 2011 at 11:16 AM, Konrad Schubert <[email protected]> wrote:
> Hi,
> I have a question about a special recursive filter problem.
>
> What I have:
>
> - given variables:
> x: time series with rather randomly occuring '0' and '1'
> wait: non negative integer
>
> - a working but ineffectiv implementation (see below)
>
> How the implementation works (what I want):
>
> The filter should drill holes of distance 'wait' between the '1' in x, e.g.
>
> x = 1 0 1 1 0 1 0 1 0 1 0 1 1 1 1
> wait = 2
>
> desired result:
>
> result = 1 0 0 1 0 0 0 1 0 0 0 1 0 0 1
>
> working implementation:
>
> #*************************************************************************
> # basic informations
> #*************************************************************************
>
> # length of input vector
> lengthX <- length(x)
>
> # stop times for the recursive filter indices
> stopS <- 1:lengthX + wait - 1
>
> # initialize the result and the intermediate result vector
> # with additional length for recursive filtering
> result <- y <- numeric(lengthX + wait)
>
> #*************************************************************************
> # filter
> #*************************************************************************
>
> # recursive filter function
> for(i in 1:lengthX){
>
> # present ('x') and lag ('y') filtering
> ans <- x[i] + sum(y[i:stopS[i]])
>
> # check for the right filter answer
> if( ans == 1){
> y[wait + i] <- -1
> result[wait + i] <- 1
> }
> }
>
> #*************************************************************************
> # post calculation
> #*************************************************************************
>
> # remove the additional length for recursive filtering
> # from the returning vector
> result <- result[-(1:wait)]
>
> -----------------------------------------------------------------------
>
> Is there anyone how has a better idea?
> Thank you for your help,
> Thomas.
> --
>
> ______________________________________________
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.