Thank you very much, Peter. The " iter[i] = x[i]" solution worked perfectly.
Galen -----Original Message----- From: Peter Langfelder [mailto:peter.langfel...@gmail.com] Sent: Monday, April 25, 2011 17:07 To: galen.a.mo...@gmail.com Cc: r-help@r-project.org Subject: Re: [R] Trouble Passing a for loop variable (iteration #) to a data frame You need to index the variable iter: instead of iter = x[i], say iter[i] = x[i]. But a better solution is to simply say iter = x at the beginning and don't update it in the loop. The way your code is written, iter just holds the last x[i], and the last x[i] at the end of each loop is 4. Peter On Mon, Apr 25, 2011 at 3:29 PM, Galen Moore <galen.a.mo...@gmail.com> wrote: > Greetings - > > > > I am working on a piece of code to simulate vehicle times in and out > in each of a number of parking spaces. At this stage, my code > basically does what it is supposed to do but for the sequential number > of each new parking event for a given space (i.e., the index of the > loop variable). Instead of passing the index of the loop variable > (iter) to the data frame, it passes the value of the total number of > iterations. Eventually, the number of iterations (parking events in a > given space) will be determined by an > rnorm() fcn, so I am not looking for a process that locks-in the > number of iterations. The total eventual data set size is small-ish, > so I'm not worried about speed. > > > > I'm sure my problem lies somehow in my setup of the data frames and my > rbind() fcns, but a great many attempts and several hours of searching > online have not yet brought success. > > > > Can you please suggest what I need to do to get the iteration # to > appear in the "iter" vector and therefore to the data frame? > > > > Many thanks, > > > > Galen > > > > > >> # fabdata3.r > >> # fabricate sample data > >> > >> #declare the mean duration > >> dur<-.04 > >> > >> #declare the stdDev of the rnorm() fcn (duration) > >> varc<-.01 > >> > >> sp<-numeric() > >> iter<-numeric() > >> ti<-numeric() > >> to<-numeric() > >> actdur<-numeric() > >> #newline<-data.frame(sp, iter, ti, to, actdur) > >> ds<-data.frame(sp, iter, ti, to, actdur) > >> > >> # BEGIN OUTER LOOP > >> for (sp in c(1:3)) { > > + > > + ct<-1 > > + # BEGIN INNER LOOP > > + > > + x <- seq(1, 4, by=1) > > + for (i in seq(along=x)) { > > + > > + if (i == 1) { > > + ti[1]<-((.333333+rnorm(1, dur, varc))) > > + to[1]<-((ti[1]+rnorm(1, dur, varc))) > > + actdur[1]<-(to[1]-ti[1]) > > + iter<-x[1] > > + > > + } > > + > > + else { > > + # set subsequent time-ins to prev to + a > + rand# > > + ti[i]<-((to[i-1]+rnorm(1, dur, varc))) > > + # calculate each event's time-out > > + to[i]<-((ti[i]+rnorm(1, dur, varc))) > > + # calculate each event's actual duration > > + actdur[i]<-(to[i]-ti[i]) > > + iter<-x[i] > > + > > + } > > + spStep<-paste("sp Value is ", sp) > > + print(spStep) > > + iterStep<-paste("iter Value is ", iter) > > + print(iterStep) > > + > > + newrow<-data.frame(sp, iter, ti, to, actdur) > > + ct=ct+1 > > + } #END INNER LOOP > > + ds<-rbind(ds, newrow) > > + } > > [1] "sp Value is 1" > > [1] "iter Value is 1" > > [1] "sp Value is 1" > > [1] "iter Value is 2" > > [1] "sp Value is 1" > > [1] "iter Value is 3" > > [1] "sp Value is 1" > > [1] "iter Value is 4" > > [1] "sp Value is 2" > > [1] "iter Value is 1" > > [1] "sp Value is 2" > > [1] "iter Value is 2" > > [1] "sp Value is 2" > > [1] "iter Value is 3" > > [1] "sp Value is 2" > > [1] "iter Value is 4" > > [1] "sp Value is 3" > > [1] "iter Value is 1" > > [1] "sp Value is 3" > > [1] "iter Value is 2" > > [1] "sp Value is 3" > > [1] "iter Value is 3" > > [1] "sp Value is 3" > > [1] "iter Value is 4" > >> print(ds) > > sp iter ti to actdur > > 1 1 4 0.3600055 0.4123550 0.05234955 > > 2 1 4 0.4343887 0.4640804 0.02969170 > > 3 1 4 0.5240268 0.5622272 0.03820032 > > 4 1 4 0.5945877 0.6436489 0.04906118 > > 5 2 4 0.3694827 0.4166405 0.04715775 > > 6 2 4 0.4609841 0.4968517 0.03586767 > > 7 2 4 0.5357721 0.5735439 0.03777185 > > 8 2 4 0.6207077 0.6512799 0.03057217 > > 9 3 4 0.3801887 0.4122605 0.03207179 > > 10 3 4 0.4440002 0.4916171 0.04761685 > > 11 3 4 0.5380228 0.5791549 0.04113214 > > 12 3 4 0.6087923 0.6291451 0.02035284 > >> # END OUTER LOOP > >> > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.