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

#### anan1986

##### New Member
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
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
___________________________________________

#### anan1986

##### New Member
thanks!! In the first place , i couldn't deal with z[0]! you answer is really helpful