mercredi 29 avril 2020

If else (set maximum to end at a set value)

How can I set a loop to run to a maximum value (Dend)? I just want to see how fast and deep it will grow but I want to set a maximum to say that it can't grow beyond Dend.

I get an error stating

  In if (D == Dend) { :
   the condition has length > 1 and only the first element will be used

Code

 D0 <- 0 
 Dend <- 4200

 r <- 5 growth rate

 days <- 1000 
 n_steps <- days*1 


 D <- rep(NA, n_steps+1)
 D <- D0

  for (time in seq_len(n_steps)){  
   if (D == Dend){
   break}  else
    D[time + 1] <- r + D[time] 
   }

   D

   plot(-D, las=1)

Aucun commentaire:

Enregistrer un commentaire