R365: Day 39- For loops

Rplot08

Busted my hand yesterday so I cant really type

For loops are useful to iterate a process for a given length of time

this example outlines how to set up a basic for loop

the cat() call (haha) might not be necessary, there might be ways around it, but i cant figure them out now

 

#based on code developed by Carol Hom
#a for loop to monitor the progress of a gut parasite
#pretend i had more than you initially, but i am fairly resiliant against the parasites
#set initial parameters for parasite populations
me.init=800;
you.init=10;
#set a vector to record the populations as the for-loop progresses
me=rep(1,100);
you=rep(1,100);
#set initial population. me is a vector so me[] tells you which point in the vector to set as the initial
me[1]=me.init;
you[1]=you.init;
#the for loop has to be of length me. One way to get around that is to set it to length(me) instead of 30
for (t in 2:length(me)){
 #set howthe pop will grow. you could also use me[1] instead of me.init
 me[t]=me[1]*(0.99)^t
 you[t]=you[1]*(1.01)^t
 #catenulate time, pop me, pop you, and end the line
 cat(t,me[t],you[t],"\n");
 #dontforget to close out
}
#create a matrix called data by binding the two columns called me and you
data=cbind(log(me),log(you));
data;
#plot the log of the parasite loads in me and you
matplot(1:100,data,pch=20,25,xlab="time",ylab="log parasite load",type="b");
Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s