ABSTRACT

This chapter begins with examples of performing basic arithmetic operations on vectors, with demonstrations (and some suggestions) on how the recycling feature can be applied quite nicely to performing computations with formulas. Next, examples of various applications of the sapply function to lists, and uses of the tapply, by and aggregate functions on data frames are given. These are followed by demonstrations of using the apply and sweep functions on a 3-dimensional array. The computations performed using the various “apply” functions are then duplicated so as to provide a demonstration of a for-loop and nested for-loops. This is followed by careful discussions, with examples, of using if-then and if-then-else constructs to embed computational flow features in code, as well as examples of applications of the very useful switch function. The chapter closes with some ideas and key features of constructing functions in R through an example of a function that finds later use.