Excel formula in R code
===

I am trying to convert an excel formula into R. Here is the description what I am trying to do. I have two column say "A" and "B" with 300 rows. col "B" is constant and the value is 2/21 and col "A" contains values (any values). Another col C that initially contains all zero. Now I am writing this formula at cell C2:

IF(C3=0,AVERAGE(A2:A21),A2*B2)+IF(C22=0,AVERAGE(C3:C21),C3*(1-B2))

Since I am giving condition on col C and calculating values for the same col C, it will updates values automatically (by default excel rule). Now the problem is when I am trying to reproduce the value of col C using R I cannot write the exact code, in R I CANNOT write the code where the values of C will be dynamically updates. Any help would be appreciated. Thanks.