26.2 For loops

Each loop has three components:

  • output - Allocate enough space for your for loop. A loop that grows at each iteration will be very “slow”.

  • sequence - What to loop over.

    • seq_along() is a safe version of the familiar 1:length(x): if you have a zero-length vector, it will tell you.
  • body - This is the code that does the work.