Skip to content

Commit

Permalink
Update 4.iterations.md
Browse files Browse the repository at this point in the history
  • Loading branch information
MattBixley authored Feb 24, 2025
1 parent 64032d4 commit cc614bd
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion docs/4.iterations.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ As shown, `for` loops and `map()` are incredibly powerful tools. However, there

Iterating over indices of a vector, instead of directly iterating on the vector, allows for flexibility and predictable outputs. Vector indices are always sequential, thus you can be confident of which element is being processed, especially if it involves multiple inputs. The sequential nature of indices also mean that outputs are always in the order of the inputs, allowing you to safely join/bind them to existing data used as inputs. In addition, if you have data that contain extra information stored as attributes (discussed in next episode), you can safely re-assign them to the output for downstream use.

### Thy can grow vectors (with constraints)
### They can grow vectors (with constraints)

As mentioned above, it is good practice to pre-assign vectors to store outputs of iterations (if desired). This ensures that R pre-allocates some amount of memory for the outputs. Whether or not it is a sufficient pre-allocation depends on the data structure of the outputs (see below). Even if the output vector is not strictly known (perhaps there are `if-else` statements that control which iteration is stored), an educated guess (with extra length for margin of error) is still better than nothing. Remember, `NA` can be filtered out later. Furthermore, a benefit of knowing (roughly) how many iterations required means that the index of the output can be used as the iterator.

Expand Down

0 comments on commit cc614bd

Please sign in to comment.