4 Basic R
ambiguity. Things might seem obscure at first. Be patient and continue. With plenty
of repetitions and examples, the fog will clear. Being an introductory chapter, we are
occasionally compelled to make simple and sweeping statements about what R does
or does not do. As you become familiar with R, you will realize that some of our
statements may be slightly (but not grossly) inaccurate. In addition to R, we discuss
some abstract concepts such as classes and objects. The motivation for this seemingly
unrelated material is this: When you start with R, many of the commands (calls to
functions) and the results they produce may mystify you. With knowledge of the gen-
eral concepts introduced here, your R sessions will be clearer. To get the most out
of this chapter, follow this strategy: Read the whole chapter lightly and do whatever
exercises and examples you find easy. Do not get stuck in a particular issue. Later, as
we introduce R code, revisit the relevant topics in this chapter for further clarification.
It makes little sense to read this chapter without actually implementing the code
and examples in a working R session. Some of the material in this chapter is based on
the excellent tutorial by Venables et al. (2003). Except for starting R, the instructions
and examples apply to all operating systems that R is implemented on. System specific
instructions refer to the Windows operating systems.
1.1 Preliminaries
In this section we learn how to work with R. If you have not yet done so, go to
http://r-project.org and download and install R. Next, start R. If you installed R
properly, click on its icon on the desktop. Otherwise, click on Start | Programs | R | R
x.y.z where x.y.z refers to the version number you installed. If all else fails, find where
R is installed (usually in the Program Files directory). Then go to the bin directory
and click on the Rgui icon. Once you start R, you will receive a welcome statement
that ends with a prompt (usually >).
1.1.1 An R session
An R session consists of starting R, working with it and then quitting. You quit R
by typing q() or (in Windows) by selecting File | Exit.
1
R displays the prompt at the
beginning of the line as a cue for you. This is where you interact with the system
(you do not type the prompt). The vast majority of our work with R will consist of
executing functions. For now, we will say that a function is a small, single-purpose
executable program that R can be instructed to run. A function is identified by a
name followed by parentheses. Often the parentheses are separated by words. These
are called arguments. We say that we execute a function when we type the function’s
name, with the parentheses (and possibly arguments) and then hit the Enter key.
Let us go through a simple session. This will give you a feel for R. At any point,
if you wish to quit, type q() to end the session. To quit, you can also use the File |
Exit menu. At the prompt, type (recall that you do not type the prompt):
>
help.start()
1
As a rule, while working with R, we avoid menus and graphical interface and stick with
the command line interface.