[R] dependent nested for loops in R
Duncan Murdoch
murdoch@dunc@n @end|ng |rom gm@||@com
Mon Feb 1 14:26:36 CET 2021
On 01/02/2021 7:03 a.m., Shaami wrote:
> z = NULL
> p = 0.25
> x = rnorm(100)
> z[1] = p*x[1] + (1-p)*5
> for(i in 2:100)
> {
> z[i] = p*x[i]+(1-p)*z[i-1]
> }
That's the same as
p <- 0.25
x <- rnorm(100)
z <- stats::filter(p*x, 1-p, init = 5, method="recursive")
This leaves z as a time series; if that causes trouble, follow it with
z <- as.numeric(z)
Duncan Murdoch
More information about the R-help
mailing list