In This Chapter
Making choices based on conditions
Looping over different values
Applying functions row-wise and column-wise
Applying functions over values, variables, and list components
A function can be nothing more than a simple sequence of actions, but these kinds of functions are highly inflexible. Often, you want to make choices and take action dependent on a certain value.
Choices aren’t the only things that can be useful in functions. Loops can prevent you from having to rewrite the same code over and over again. If you want to calculate the summary statistics on different datasets, for example, you can write the code to calculate those statistics and then tell R to carry out that code for all the datasets you had in mind.
R has a very specific mechanism for looping over values that combines a lot of power with a minimum of unwanted side effects. Instead of using a classic loop structure, you use a function to apply another function on any of the objects discussed in the previous chapters. This way of looping over values is one of the ...