The Art of R Programming

(WallPaper) #1

shell commands using pipes.) The ability to combine R functions gives tre-
mendous flexibility and, if used properly, is quite powerful. As a simple
example, consider this (compound) command:


nrow(subset(x03,z == 1))


First, thesubset()function takes the data framex03and extracts all
records for which the variablezhas the value 1. This results in a new frame,
which is then fed to thenrow()function. This function counts the number
of rows in a frame. The net effect is to report a count ofz= 1 in the original
frame.
The termsobject-oriented programmingandfunctional programmingwere
mentioned earlier. These topics pique the interest of computer scientists,
and though they may be somewhat foreign to most other readers, they are
relevant to anyone who uses R for statistical programming. The following
sections provide an overview of both topics.


Object-Oriented Programming


The advantages of object orientation can be explained by example. Con-
sider statistical regression. When you perform a regression analysis with
other statistical packages, such as SAS or SPSS, you get a mountain of out-
put on the screen. By contrast, if you call thelm()regression function in
R, the function returns anobjectcontaining all the results—the estimated
coefficients, their standard errors, residuals, and so on. You then pick and
choose, programmatically, which parts of that object to extract.
You will see that R’s approach makes programming much easier, partly
because it offers a certain uniformity of access to data. This uniformity stems
from the fact that R ispolymorphic, which means that a single function can
be applied to different types of inputs, which the function processes in the
appropriate way. Such a function is called ageneric function. (If you are a C++
programmer, you have seen a similar concept invirtual functions.)
For instance, consider theplot()function. If you apply it to a list of
numbers, you get a simple plot. But if you apply it to the output of a
regression analysis, you get a set of plots representing various aspects of
the analysis. Indeed, you can use theplot()function on just about any
object produced by R. This is nice, since it means that you, as a user, have
fewer commands to remember!


Functional Programming


As is typical in functional programming languages, a common theme in R
programming is avoidance of explicit iteration. Instead of coding loops,
you exploit R’s functional features, which let you express iterative behavior
implicitly. This can lead to code that executes much more efficiently, and it
can make a huge timing difference when running R on large data sets.


Introduction xxi
Free download pdf