mercredi 4 mars 2020

Once statement in R i.e. evaluate if statement only once

I am looking for a method in R to run the block inside the if statement only the first time the if statement is evaluated as TRUE, but the block would not be run again even if the if condition is TRUE again. Specifically, the method would be useful in a loop. This would be the "once" statement (it is called so in some exotic languages).

Example:

for (id in id_list){ # runs over a list of several id's which are random
   if (id == "snake"){ # I want to run this block only the first time and NOT each time id == "snake" 
      # now, do some calculations
      # ...
   }
   # do some other calculations by default for all other runs inside the loop
   # ...
}

I would be also curious to know how would this work in Python.

Aucun commentaire:

Enregistrer un commentaire