[R] Iteration within data blocks

Joerg Maeder joerg.maeder at ethz.ch
Wed Oct 31 09:15:49 CET 2001


hello,

at the moment i found only a solution that works with a single loop for
the scenzarios

#the dataset (i used the first letter for colum names)
da <-
cbind(s=c(1,1,1,1,2,2,2,2),y=c(1,2,3,4,1,2,3,4),g=c(.05,.03,.01,0,.01,.01,.01,0),v=c(1,0,0,0,1,0,0,0))
#set the values of first year to zero (it woudn't be used for
calculations)
da[da[,'y']==1,'g'] <- 0
#thye single loop
for (as in 1:2) 
 da[da[,'s']==as,'v'] <- cumprod(da[da[,'s']==as,'g']+1)*da[da[,'s']==as
& da[,'y']==1,'v']

perhaps i will find a solution without any loops. if yes, will send it
to you

gruess

joerg


Michaell Taylor wrote:
> 
> I know there must be an easy way to do this, but I am too new to R and it
> must be late because I can't seem to get myself around this.  Assume I have,
> 
> scenario        year    growth  value
> 1               1       .05     1
> 1               2       .03     0
> 1               3       .01     0
> 1               4       0       0
> 2               1       .01     1
> 2               2       .01     0
> 2               3       .01     0
> 2               4       0       0
> 
> What I want to do is within each scenario "grow" the value by the growth rate
> so that I get :
> scenario        year    growth  value
> 1               1       .05     1
> 1               2       .03     1.03
> 1               3       .01     1.0403
> 1               4       0       1.0403
> 2               1       .01     1
> 2               2       .01     1.01
> 2               3       .01     1.0201
> 2               4       0       1.0201
> 
> There are 5,000 scenarios and 10 years. The only thing dancing in my head are
> giant for loops, which I know isn't right.
> 
> Any suggestions?
> 
> =========================================
> Michaell Taylor, PhD
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
    Joerg Maeder             IACETH              INSTITUTE
   PhD Student                              FOR ATMOSPHERIC 
  Phone: +41 1 633 36 25                 AND CLIMATE SCIENCE
 Fax: +41 1 633 10 58                  ETH ZÜRICH Switzerland
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list