Functions and Iteration in R
At this point in your R language acquisition, you are able to write scripts that perform tasks like cleaning a dataset, fitting a series of models, producicing plots for residual diagnostics, and even simulating datasets.
(If you are new to R, see our Introduction to R and Data Wrangling with R materials.)
A common problem at this stage is that we are are copying and pasting code to perform repetitive tasks like
- Calculating the standard deviation of each column in a dataframe
- Calculating something without a predefined function, like the standard error, of each column in a dataframe
- Generating random data repeatedly
- Reading and writing sets of files
The primary issues with the copy-paste approach to repetitive tasks are
- Scaling up: It is fine if we need to make 5 graphs, but not 50.
- Updating: Changing one thing in a process we repeat n times requires changing it n times. It may be a typo we missed earlier, or it may be that we want to change something like the color of a line or the number of decimal places. This is tedious and makes room for mistakes.
- Organization: Scripts can quickly become unwieldy and difficult to navigate and maintain when they have repeated code.
Functions and iteration solve these issues by making procedures compact and portable (functions) and allowing us to repeat those procedures across sets of objects (iteration).