[R] Output from while and for loop
Charles C. Berry
cberry at tajo.ucsd.edu
Wed Aug 22 20:24:15 CEST 2007
On Tue, 21 Aug 2007, Ryan Briscoe Runquist wrote:
>
> Hello,
>
> I am new and am having a hard time getting the proper syntax for output
> from loops. I am working on a simulation to generate a null expectation of
> bee behavior. Pieces of it work. The part that I am having specific
> difficulty is in output of a vector from within the while loop that I am
> using.
Hmmm. I think I can guess at your problem.
You need to assign the result to a vector or list element along these
lines:
my.list <- list()
k <- 0
while( keep.going ){
k <- k+1
## do lots of stuff
## details omitted
my.list[[ k ]] <- result.of.doing.lots.of.stuff
}
unlist( my.list ) returns a vector if result.of.doing.lots.of.stuff is
atomic.
There are lots of variations on this. If the result ('my.list') will be
very long and each element is atomic, it helps speed to set it up a vector
ahead of time and then fill one element per pass thru the loop.
Basically the simulation works as such: I have a starting point
> and a neighbor matrix and a certain threshold distance for travel. In the
> while loop the "bee" moves to a randomly chosen neighbor location. I want
> to be able to record the elevations of these points (including the starting
> point) so that I can look at variance in elevation and mean elevation. The
> loop itself works as does the calculation of the final elevation list,
> change in elevation list, and true total distance traveled. I have looked
> in all of the email archives but have not come across a correct way of
> doing it. Code below:
>
> start.elev.list<-list()
> final.mean.elev.list<-list()
> final.elev.list<-list()
> final.distance.list<-list()
> final.delta.elev.list<-list()
> final.var.elev<-list()
>
>
> b<-length(Bees.Day.1$bee)
> for (bee in 1:b){
> #this is for number of bees that are trackable in the day with starting
> points and threshold distances
> elev.current.vector<-vector(mode="numeric", length=0)
> count<-1
> ElevSS<-0
> d.traveled<-0
> thresh<-Bees.Day.1$cum.dist[bee]
> n<-Bees.Day.1$grid.pt[bee]
> #I'm making this up for the threshold, want to be bee specific
> #current.point<-round(runif(1,1,n)) #random starting point
> current.point<-Day.1.neighbor.matrix[1,n]
> #I want to specify the first point in the matrix
> Elev.Sum<-Day.1.elev.vector[current.point]
>
>
> while(d.traveled<thresh){
> #which of the four options will be selection
> transition<-round(runif(1,1,4))
>
> #so, what's the new point?
> new.point<- Day.1.neighbor.matrix[transition,n]
>
> #what is the variance in elevation changed
> Elev.current<-Day.1.elev.vector[current.point]
> elev.current.vector[i]<-Elev.current
> Elev.new<-Day.1.elev.vector[new.point]
> Elev.Sum<-(Elev.Sum+Elev.new)
>
> #how far will bee travelled
> current.travel<- Day.1.distance.matrix[current.point, new.point]
> d.traveled<- current.travel + d.traveled
> current.point<- new.point
>
> #Number of iterations until we reach the threshold
> count<-count+1
>
> }
>
> print(count)
> print(elev.current.vector)
> mean.elev<-Elev.Sum/count
> print(paste("Final mean elev for bee", bee, "is", mean.elev, sep=" "))
> final.mean.elev.list[bee]<-list(mean.elev)
>
> #What was the start elevation?
> start.elev<-Day.1.elev.vector[n]
> print(paste("Start elev for bee",bee,"is",start.elev, sep=" "))
> start.elev.list[bee]<-list(start.elev)
>
> #what is the final elevation?
> final.elev<-Day.1.elev.vector[current.point]
> print(paste("Final elev for bee",bee,"is", final.elev,sep=" "))
> final.elev.list[bee]<-list(final.elev)
>
> print(paste("Final travel distance for bee", bee,"is", d.traveled, sep=" "))
> final.distance.list[bee]<-list(d.traveled)
>
> net.delta.elev<-(final.elev-Day.1.elev.vector[n])
> print(paste("Final net change in elevation for bee",bee,"is",
> net.delta.elev,sep=" "))
> final.delta.elev.list[bee]<-list(net.delta.elev)
>
> }
> ~~~~~~~~~~~~~~~~~~
> Ryan D. Briscoe Runquist
> Population Biology Graduate Group
> University of California, Davis
> rdbriscoe at ucdavis.edu
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
More information about the R-help
mailing list