The Art of R Programming

(WallPaper) #1
not require interaction with the user. It’s useful for production jobs, such as
when a program must be run periodically, say once per day, because you can
automate the process.

1.1.1 Interactive Mode................................................


On a Linux or Mac system, start an R session by typingRon the command
line in a terminal window. On a Windows machine, start R by clicking the
R icon.
The result is a greeting and the R prompt, which is the>sign. The
screen will look something like this:

R version 2.10.0 (2009-10-26)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0


Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

>

You can then execute R commands. The window in which all this
appears is called the Rconsole.
As a quick example, consider a standard normal distribution—that is,
with mean 0 and variance 1. If a random variableXhas that distribution,
then its values are centered around 0, some negative, some positive, averag-
ing in the end to 0. Now form a new random variableY=|X|. Since we’ve
taken the absolute value, the values ofYwillnotbe centered around 0, and
the mean ofYwill be positive.
Let’s find the mean of Y. Our approach is based on a simulated example
ofN(0,1) variates.

> mean(abs(rnorm(100)))
[1] 0.7194236

This code generates the 100 random variates, finds their absolute values,
and then finds the mean of the absolute values.
The[1]you see means that the first item in this line of output is item 1.
In this case, our output consists of only one line (and one item), so this is re-
dundant. This notation becomes helpful when you need to read voluminous
output that consists of a lot of items spread over many lines. For example, if
there were two rows of output with six items per row, the second row would
be labeled[7].

> rnorm(10)
[1] -0.6427784 -1.0416696 -1.4020476 -0.6718250 -0.9590894 -0.8684650
[7] -0.5974668 0.6877001 1.3577618 -2.2794378

2 Chapter 1

Free download pdf