Help! I need to use R to calculate a recursive function!

#1
hi,all Z(i)=0.05X(i)+0.95Z(i-1), where i = 1.....32, and X's are given data, the question is how to use r to get the 32 values of Z. I couldn't do this in R and wish somebody can help me.
assume z(0)=175

thanks in regards
 

mp83

TS Contributor
#2
First of all keep in mind that you can't index 0 in R. You can modify the following to meet your needs.

PHP:
> x=rnorm(5)
> z=rep(NA,5)
> z[1]<-175
> for (i in 2:5) {
+ z[i]<-.05*x[i]+.95*z[i-1]}
> z
[1] 175.0000 166.2698 157.8924 149.9871 142.4933
> x
[1] -0.9176116  0.3953809 -1.2777427 -0.2138265  0.1121403
___________________________________________