[R] Output from while and for loop
Ryan Briscoe Runquist
rdbriscoe at ucdavis.edu
Wed Aug 22 01:34:58 CEST 2007
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. 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
More information about the R-help
mailing list