<?xml version="1.0"?>
<!DOCTYPE html    PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN"
           "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="GENERATOR" content="TtM 3.70" />
 <style type="text/css">
 div.p { margin-top: 7pt; }
 span.roman {font-family: serif; font-style: normal; font-weight: normal;} 
</style>
 

    
<title> An introduction to \R for ecological modeling (lab 1) </title>
</head>
<body>
 
<h1 align="center">An introduction to  R&nbsp;for ecological modeling (lab 1) </h1>

<h3 align="center">Aug 30, 2005
</h3>

<h3 align="center">Stephen Ellner <a href="#tthFtNtAAB" name="tthFrefAAB">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msup><mrow></mrow><mrow><mn>1</mn></mrow>
</msup>
</mrow></math></a>,
modified by Ben Bolker <a href="#tthFtNtAAC" name="tthFrefAAC">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msup><mrow></mrow><mrow><mn>2</mn></mrow>
</msup>
</mrow></math></a> </h3>

<div class="p"><!----></div>
  <h2><a name="tth_sEc0">
0</a>&nbsp;&nbsp;How to use this document</h2>

<div class="p"><!----></div>

<ul>
<li> These notes contain many sample calculations. It is important to 
do these yourself-<b>type them in at your keyboard and see what
happens on your screen</b>-to get the 
feel of working in  R.
<div class="p"><!----></div>
</li>

<li> <b>Exercises</b> in the middle of a section should be done
immediately when you get to them, and make sure you have them right 
before moving on. Some more challenging exercises (indicated by asterisks or
identified as a Project) are given at the end of some sections. These
can be left until later, and may be assigned as homework.
<div class="p"><!----></div>
</li>
</ul>

<div class="p"><!----></div>
These notes are based in part on course materials by former TAs
Colleen Webb, Jonathan Rowell and Daniel Fink at Cornell, Professors
Lou Gross (University of Tennessee) and Paul Fackler (NC State
University), and on the book <em>Getting Started with Matlab</em> by
Rudra Pratap (Oxford University Press).  It also draws on the
documentation supplied with  R.

<div class="p"><!----></div>
 <h2><a name="tth_sEc1">
1</a>&nbsp;&nbsp;What is  R?</h2>
 R&nbsp;is an object-oriented scripting language that combines 

<ul>
<li> a programming language called  S,
developed by John Chambers at Bell Labs, that can be used for 
numerical simulation of deterministic and stochastic dynamic models
<div class="p"><!----></div>
</li>

<li> an extensive set of functions for classical and modern
  statistical data analysis and modeling
<div class="p"><!----></div>
</li>

<li> graphics functions for visualizing data and model output
<div class="p"><!----></div>
</li>

<li> a user interface with a few basic menus and extensive help
facilities
<div class="p"><!----></div>
</li>
</ul>

<div class="p"><!----></div>
 R&nbsp;is an open source project, available for free download via the
Web. Originally a research project in statistical computing
[<a href="#IhakaGentleman1996" name="CITEIhakaGentleman1996">2</a>], it is now managed by a development team
that includes a number of well-regarded statisticians, and is widely
used by statistical researchers (and a growing number of theoretical
ecologists and ecological modellers) as a platform for making new
methods available to users.  The commercial implementation of  S
(called  S-PLUS) offers an Office-style "point and click"
interface that  R&nbsp;lacks. For our purposes, however, the advantage of
this front-end is outweighed by the fact that  R&nbsp;is built on a faster
and much less memory-hungry implementation of  S&nbsp;and is easier to
interface with other languages. A standard installation of of  R&nbsp;also
includes extensive documentation, including an introductory manual
(
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mo>&ap;</mo><mn>100</mn></mrow></math> pages) and a comprehensive reference manual (over 1000
pages).  (There is a semi-inclusive graphical front-end for  R, called
 Rcmdr, available at the  R&nbsp;site, but we will not be using it in
this class.)

<div class="p"><!----></div>
     <h3><a name="tth_sEc1.1">
1.1</a>&nbsp;&nbsp;Installing  R&nbsp;on your computer</h3>
The main source for  R&nbsp;is the CRAN home page <a href="http://cran.r-project.org"><tt>http://cran.r-project.org</tt></a>. You
can get the source code, but most users will prefer a precompiled 
version. To get one of these from CRAN, click on the link for your OS, 
continue to the folder corresponding to your OS version, 
and from there to the download file (e.g. <tt>base/rwxxxx.exe</tt> for 
Windows, <tt>rmxxx.sit</tt> for under MacOS, where <tt>xxxx</tt> is the 
version number). 

<div class="p"><!----></div>
<em>For Windows, you can also use one of the CDs I've burned for the class: you'll find
the <tt>rw2011.exe</tt> file on the CD.  Note that this corresponds
(slightly confusingly) to  R&nbsp;version 2.1.1&nbsp;(not 20.11 or 2.011).
You will also find pre-compiled versions of a large number of packages
from CRAN on the CD, as well as a variety of other tools and
documents.</em>

<div class="p"><!----></div>
The standard distributions of  R&nbsp;include several <em>packages</em>,
user-contributed suites of add-on functions (unfortunately, the
command to load a package into  R&nbsp;is <tt>library()</tt>!).  These
Notes use some packages that are not part of the standard
distribution. In the Windows version additional packages can be
installed easily from within  R&nbsp;using the <b>Packages</b> menu.
Only some of the packages are available pre-compiled for Unix/Linux
and MacOS X. For other packages in Unix/Linux you have to download
and compile the source code.

<div class="p"><!----></div>
For Windows,  R&nbsp;is installed by launching the downloaded file and 
following the on-screen instructions. At the end you'll have an  R&nbsp;icon on
your desktop that can be used to launch the program. Installing versions for
Linux or Unix is more complicated and idiosyncratic, which will not bother the
corresponding users.  (This introduction is generally moderately Windows-specific,
although we've tried to mark Windows-specific items with a .)

<div class="p"><!----></div>
&nbsp;If you are using  R&nbsp;on a machine
where you have sufficient permissions, you may want to
edit some of your graphical user interface (GUI) options.

<ul>
<li> To allow command and graphics windows to move
independently on the desktop (SDI, single-document
interface, rather than MDI, multiple-document
interface): go to <tt>File/Edit/Preferences</tt> and click the
radio button to set <tt>SDI</tt> 
instead of <tt>MDI</tt>.  This edits the <tt>Rconsole</tt>
file.  R&nbsp;will ask you where to save it; click
through to <tt>My Computer/Program Files/R/rwxxxx/etc</tt>,
where <tt>rwxxxx</tt> stands for the version of  R.
You will then need to restart  R.
<div class="p"><!----></div>
</li>

<li>To select the most powerful version of the help system,
go to the same directory (<tt>My Computer/Program Files/R/rwxxxx/etc</tt>)
and use Notepad to edit the <tt>Rprofile</tt> file
to un-comment <tt>options(chmhelp=TRUE)</tt>
by removing the # at the start of the line.
<div class="p"><!----></div>
</li>
</ul>

<div class="p"><!----></div>

<div class="p"><!----></div>
     <h3><a name="tth_sEc1.2">
1.2</a>&nbsp;&nbsp;Starting  R</h3>

<div class="p"><!----></div>
&nbsp;Just click on the icon on your desktop, or in the <tt>Start</tt>
menu (if you allowed the Setup program to make either or both of these).
If you lose these shortcuts for some reason, you can search for
the executable file <tt>Rgui.exe</tt> on your hard drive, which will probably be somewhere
like <tt>Program&nbsp;Files\R\rwxxx\bin\Rgui.exe</tt>.

<div class="p"><!----></div>
     <h3><a name="tth_sEc1.3">
1.3</a>&nbsp;&nbsp;Stopping  R</h3>

<div class="p"><!----></div>
Lebanese proverb: "when entering, always look
for the exit".  You can stop  R&nbsp;from the <tt>File</tt> menu
(), or you can stop it by typing <tt>q()</tt> at the 
command prompt (if you type <tt>q</tt> by itself, you will get some
confusing output which is actually  R&nbsp;trying to tell you the
definition of the <tt>q</tt> function; more on this later).

<div class="p"><!----></div>
When you quit,  R&nbsp;will ask you if you want to save the workspace
(that is, all of the variables you have defined in this session); for
now (and in general), say "no" in order to avoid clutter.

<div class="p"><!----></div>
Should an  R&nbsp;command seem to be stuck or
take longer than you're willing to wait, click on the stop sign on
the menu bar or hit the <tt>Escape</tt> key (in Unix, type Control-C).

<div class="p"><!----></div>
 <h2><a name="tth_sEc2">
2</a>&nbsp;&nbsp;Interactive calculations</h2>
&nbsp;When you start  R&nbsp;it opens the <b>console</b> window.
The console has a few basic menus at the top; check them out on your own. The console is 
also where you enter commands for  R&nbsp;to execute
<em>interactively</em>, meaning that the command is executed and 
the result is displayed as soon as you hit the <tt>Enter</tt> key. For example, at 
the command prompt <tt>&#62;</tt>, type in <tt>2+2</tt> and hit <tt>Enter</tt>; you will see 

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;2&nbsp;+&nbsp;2
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;4

&nbsp;
</pre> </font>

<div class="p"><!----></div>
(When cutting and pasting from this document to  R, don't include the
text for the command prompt (<tt>&#62;</tt>).)

<div class="p"><!----></div>
To do anything complicated, the results from calculations have to be stored 
in (<em>assigned to</em>) variables. For example:

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;a&nbsp;=&nbsp;2&nbsp;+&nbsp;2
&nbsp;
</pre> </font>

<div class="p"><!----></div>
 R&nbsp;automatically creates the variable <tt>a</tt> and stores the result (4)
in it, but  R&nbsp;doesn't print anything.  This may seem strange, but you'll
often be creating and manipulating huge sets of data that would fill
many screens, so the default is to <em>not</em> print the results.
To ask  R&nbsp;to print the value, just type the variable name by itself

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;a
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;4

&nbsp;
</pre> </font>

<div class="p"><!----></div>
(the <tt>[1]</tt> at the beginning of the line is just  R&nbsp;printing
an index of element numbers; if you print a result that
displays on multiple lines,  R&nbsp;will put an index at the beginning
of each line.  <tt>print(a)</tt> also works to print the value of
a variable.)  By default, a variable created this way is a <em>vector</em> (an ordered
list), and it is <em>numeric</em> because we gave  R&nbsp;a number rather
than (e.g.) a character string like <tt>"pxqr"</tt>;
in this case <tt>a</tt> is a numeric vector of length 1,
which acts just like a number. 

<div class="p"><!----></div>
You could also type <tt>a=2+2; a</tt>,
using a semicolon to put two or more commands on a single line.
Conversely, you can break lines <b>anywhere that  R&nbsp;can tell you haven't
finished your command</b> and  R&nbsp;will give you a "continuation" prompt
(<tt>+</tt>) to let you know that it doesn't thinks you're finished yet: try typing

<pre>
a=3*(4+
5)

</pre>
to see what happens
(this often happens e.g. if you forget to close parentheses).
If you get stuck continuing a command you don't want-e.g. you opened
the wrong parentheses-just hit the <tt>Escape</tt> key or the stop
icon in the menu bar to get out.

<div class="p"><!----></div>
Variable names in  R&nbsp;must begin with a letter, followed by alphanumeric 
characters. You can break up long names with a period, as in
<tt>very.long.variable.number.3</tt>, or an underscore (<tt>_</tt>), but you 
can't use blank spaces in variable names.  R&nbsp;is case sensitive: <tt>Abc</tt> and <font face="helvetica">abc</font> 
are different variables.
Make variable names long enough to remember, short
enough to type.
<tt>N.per.ha</tt> or <tt>pop.density</tt> are better than <tt>x</tt>
and <tt>y</tt> (too short) or <tt>available.nitrogen.per.hectare</tt> (too long).
Avoid <tt>c</tt>, <tt>l</tt>, <tt>q</tt>, <tt>t</tt>, <tt>C</tt>, <tt>D</tt>,
<tt>F</tt>, <tt>I</tt>, and <tt>T</tt>, which are either built-in  R&nbsp;functions or
hard to tell apart.

<div class="p"><!----></div>
 R&nbsp;does calculations with variables as if they were numbers. It uses  
<tt>+</tt>, <tt>-</tt>, <tt>*</tt>, <tt>/</tt>, and <tt>^</tt>
for addition, subtraction, multiplication, division and 
exponentiation, respectively. For example:

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;x&nbsp;=&nbsp;5
&#62;&nbsp;y&nbsp;=&nbsp;2
&#62;&nbsp;z1&nbsp;=&nbsp;x&nbsp;*&nbsp;y
&#62;&nbsp;z2&nbsp;=&nbsp;x/y
&#62;&nbsp;z3&nbsp;=&nbsp;x^y
&#62;&nbsp;z2
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;2.5

&nbsp;
</pre> </font>
  <font color="#FF0000">
<pre>
&#62;&nbsp;z3
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;25

&nbsp;
</pre> </font>

<div class="p"><!----></div>
Even though  R&nbsp;did not display the values of <tt>x</tt> and <tt>y</tt>, it "remembers" that 
it assigned values to them. Type <tt>&nbsp;&gt;&nbsp;x;&nbsp;y&nbsp;</tt> to display the values. 

<div class="p"><!----></div>
You can retrieve and edit previous commands.
The up-arrow ( 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mo stretchy="false">&uarr;</mo></mrow></math> ) key, or
<tt>Control-P</tt>) recalls previous 
commands to the prompt. For example, you can bring back the second-to-last command and edit it to

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;z3&nbsp;=&nbsp;2&nbsp;*&nbsp;x^y
&nbsp;
</pre> </font>

<div class="p"><!----></div>
(experiment with the 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mo stretchy="false">&darr;</mo></mrow></math>, 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mo>&rarr;</mo></mrow></math>, 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mo>&larr;</mo></mrow></math>, <tt>Home</tt> and <tt>End</tt> keys
too).

<div class="p"><!----></div>
You can combine several operations in one calculation:

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;A&nbsp;=&nbsp;3
&#62;&nbsp;C&nbsp;=&nbsp;(A&nbsp;+&nbsp;2&nbsp;*&nbsp;sqrt(A))/(A&nbsp;+&nbsp;5&nbsp;*&nbsp;sqrt(A))
&#62;&nbsp;C
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;0.5543706

&nbsp;
</pre> </font>

<div class="p"><!----></div>
Parentheses specify the order of operations. 
The command

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;C&nbsp;=&nbsp;A&nbsp;+&nbsp;2&nbsp;*&nbsp;sqrt(A)/A&nbsp;+&nbsp;5&nbsp;*&nbsp;sqrt(A)
&nbsp;
</pre> </font>

<div class="p"><!----></div>
is not the same as the one above; rather, it is 
equivalent to <tt>&#62; C=A + 2*(sqrt(A)/A) + 5*sqrt(A)</tt>.

<div class="p"><!----></div>
The default order of operations is: (1) parentheses; (2) exponentiation, or powers, (3) multiplication 
and division, (4) addition and subtraction ("<b>p</b>retty <b>p</b>lease
<b>m</b>y <b>d</b>ear <b>A</b>unt <b>S</b>ally").

<div class="p"><!----></div>

<table>
<tr><td align="left"><tt>&gt;&nbsp;b&nbsp;=&nbsp;12-4/2^3</tt> </td><td align="center">gives  </td><td align="left"><tt>12&nbsp;-&nbsp;4/8&nbsp;=&nbsp;12&nbsp;-&nbsp;0.5&nbsp;=&nbsp;11.5</tt> </td></tr>
<tr><td align="left"><tt>&gt;&nbsp;b&nbsp;=&nbsp;(12-4)/2^3</tt> </td><td align="center">gives </td><td align="left"><tt>8/8&nbsp;=&nbsp;1</tt> </td></tr>
<tr><td align="left"><tt>&gt;&nbsp;b&nbsp;=&nbsp;-1^2</tt>   </td><td align="center">gives  </td><td align="left"><tt>-(1^2)&nbsp;=&nbsp;-1</tt> </td></tr>
<tr><td align="left"><tt>&gt;&nbsp;b&nbsp;=&nbsp;(-1)^2</tt> </td><td align="center">gives  </td><td align="left"><tt>1</tt>
</td></tr></table>
 

<div class="p"><!----></div>
In complicated expressions it's best to <b>use parentheses to specify 
explicitly what you want</b>, such as <tt>&nbsp;&gt;&nbsp;b&nbsp;=&nbsp;12&nbsp;-&nbsp;(4/(2^3))&nbsp;</tt> 
or at least <tt>&nbsp;&gt;&nbsp;b&nbsp;=&nbsp;12&nbsp;-&nbsp;4/(2^3)&nbsp;</tt>; a few extra sets of parentheses
never hurt anything, although if you get confused
it's better to think through the order of operations rather than flailing
around adding parentheses at random.

<div class="p"><!----></div>
 R&nbsp;also has many <b>built-in mathematical functions</b> that operate on variables
(Table <a href="#MathFunctions">1</a> shows a few). You can get help on any  R&nbsp;function by entering <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <tt>?functionname</tt> <br />
in the console window (e.g., try <tt>?sin</tt>). You should also explore
the items available on the Help menu, which include the manuals, FAQs, and a Search
facility (`Apropos' on the menu) which is useful if you sort of maybe remember part of the
the name of what it is you need help on.  

<div class="p"><!----></div>

<div class="p"><!----></div>
<a name="tth_tAb1">
</a> 
<table>
<tr><td width="194"><tt>abs()</tt> </td><td width="402">absolute value </td></tr>
<tr><td width="194"><tt>cos()</tt>, <tt>sin()</tt>, <tt>tan()</tt> </td><td width="402">cosine, sine, tangent of angle x in radians</td></tr>
<tr><td width="194"><tt>exp()</tt>  </td><td width="402">exponential function, 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msup><mrow><mi>e</mi></mrow><mrow><mi>x</mi></mrow>
</msup>
</mrow></math>  </td></tr>
<tr><td width="194"><tt>log()</tt>  </td><td width="402">natural (base-
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>e</mi></mrow></math>) logarithm </td></tr>
<tr><td width="194"><tt>log10()</tt> </td><td width="402">common (base-10) logarithm </td></tr>
<tr><td width="194"><tt>sqrt()</tt>  </td><td width="402">square root </td></tr></table>


<center>Table 1: Some of the built-in mathematical functions in  R. You can
get a more complete list from the Help system: <tt>?Arithmetic</tt> for simple, 
<tt>?log</tt> for logarithmic, <tt>?sin</tt> for trigonometric, and <tt>?Special</tt> for special 
functions.</center> 
<a name="MathFunctions">
</a>

<div class="p"><!----></div>
<b>Exercise 1</b>: Using editing
shortcuts wherever you can, have  R&nbsp;compute the values of 


<ol type="1">
<li> 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<mfrac><mrow>
<msup><mrow><mn>2</mn></mrow><mrow><mn>7</mn></mrow>
</msup>
</mrow>
<mrow>
<msup><mrow><mn>2</mn></mrow><mrow><mn>7</mn></mrow>
</msup>
<mo>-</mo><mn>1</mn></mrow>
</mfrac>
</mrow></math>
and compare it with 

<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mo stretchy="false">(</mo><mn>1</mn><mo>-</mo>
<mfrac><mrow><mn>1</mn></mrow>
<mrow>
<msup><mrow><mn>2</mn></mrow><mrow><mn>7</mn></mrow>
</msup>
</mrow>
</mfrac>

<msup><mrow><mo stretchy="false">)</mo></mrow><mrow><mo>-</mo><mn>1</mn></mrow>
</msup>
</mrow></math>
(If any square brackets [] show up in your web browser, replace
them with regular parentheses ().)
<div class="p"><!----></div>
</li>

<li> 

<ul>
<li> 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mn>1</mn><mo>+</mo><mn>0</mn><mo>.</mo><mn>2</mn></mrow></math>
<div class="p"><!----></div>
</li>

<li> 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mn>1</mn><mo>+</mo><mn>0</mn><mo>.</mo><mn>2</mn><mo>+</mo><mn>0</mn><mo>.</mo>
<msup><mrow><mn>2</mn></mrow><mrow><mn>2</mn></mrow>
</msup>
<mo stretchy="false">/</mo><mn>2</mn></mrow></math>
<div class="p"><!----></div>
</li>

<li> 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mn>1</mn><mo>+</mo><mn>0</mn><mo>.</mo><mn>2</mn><mo>+</mo><mn>0</mn><mo>.</mo>
<msup><mrow><mn>2</mn></mrow><mrow><mn>2</mn></mrow>
</msup>
<mo stretchy="false">/</mo><mn>2</mn><mo>+</mo><mn>0</mn><mo>.</mo>
<msup><mrow><mn>2</mn></mrow><mrow><mn>3</mn></mrow>
</msup>
<mo stretchy="false">/</mo><mn>6</mn></mrow></math>
<div class="p"><!----></div>
</li>

<li> 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msup><mrow><mi>e</mi></mrow><mrow><mn>0</mn><mo>.</mo><mn>2</mn></mrow>
</msup>
</mrow></math> (remembering that  R&nbsp;knows <tt>exp()</tt> but not 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>e</mi></mrow></math>;
how would you get  R&nbsp;to tell you the value of 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>e</mi></mrow></math>?  What is the
point of this exercise?)
<div class="p"><!----></div>
</li>
</ul>
<div class="p"><!----></div>
</li>

<li>the standard normal probability density,

<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<mfrac><mrow><mn>1</mn></mrow>
<mrow><msqrt><mrow><mn>2</mn><mi>&pi;</mi></mrow></msqrt></mrow>
</mfrac>

<msup><mrow><mi>e</mi></mrow><mrow><mo>-</mo>
<msup><mrow><mi>x</mi></mrow><mrow><mn>2</mn></mrow>
</msup>
<mo stretchy="false">/</mo><mn>2</mn></mrow>
</msup>
</mrow></math>, for values of 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>x</mi><mo>=</mo><mn>1</mn></mrow></math>
and 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>x</mi><mo>=</mo><mn>2</mn></mrow></math> ( R&nbsp;knows 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>&pi;</mi></mrow></math> as <tt>pi</tt>.)
(You can check your answers against the built-in function
for the normal distribution; <tt>dnorm(c(1,2))</tt> should give
you the values for the standard normal for 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>x</mi><mo>=</mo><mn>1</mn></mrow></math> and 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>x</mi><mo>=</mo><mn>2</mn></mrow></math>.)
<div class="p"><!----></div>
</li>
</ol>

<div class="p"><!----></div>
<b>Exercise 2</b>: Do an Apropos on <tt>sin</tt> via the Help
menu, to see what it does. Now enter the command

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;help.search("sin")
&nbsp;
</pre> </font>

<div class="p"><!----></div>
and see what that does (answer: <tt>help.search</tt> pulls up all help
pages that include `sin' anywhere in their title or text. Apropos just looks at the name of the function).
If you have a net connection, try <tt>RSiteSearch("sin")</tt> from the command
line or the equivalent from the menu and see what happens. 

<div class="p"><!----></div>
 <h2><a name="tth_sEc3">
3</a>&nbsp;&nbsp;A first interactive session: linear regression</h2>
To get a feel for working in  R&nbsp;we'll fit a straight-line model (linear 
regression) to data. Below are some data on the maximum growth rate 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msub><mrow><mi>r</mi></mrow><mrow>
<mtext>max</mtext>
</mrow>
</msub>
</mrow></math> of laboratory populations 
of the green alga <em>Chlorella vulgaris</em> as a function of light intensity (
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>&mu;</mi></mrow></math>E per
m
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msup><mrow></mrow><mrow><mn>2</mn></mrow>
</msup>
</mrow></math> per second). These 
experiments were run during the system-design phase of the study reported by
Fussmann et al. [<a href="#Fussman+2000" name="CITEFussman+2000">1</a>].

<div class="p"><!----></div>
Light: 20,  20,  20,  20,  21,  24,  44,  60,  90,  94, 101 <br />

<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msub><mrow><mi>r</mi></mrow><mrow>
<mtext>max</mtext>
</mrow>
</msub>
</mrow></math>: 1.73, 1.65, 2.02, 1.89, 2.61, 1.36, 2.37, 2.08, 2.69, 2.32, 3.67 <br />

<div class="p"><!----></div>
To analyze these data in  R, first enter them as numerical <em>vectors</em>: 

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;Light&nbsp;=&nbsp;c(20,&nbsp;20,&nbsp;20,&nbsp;20,&nbsp;21,&nbsp;24,&nbsp;44,&nbsp;60,&nbsp;90,&nbsp;94,&nbsp;101)
&#62;&nbsp;rmax&nbsp;=&nbsp;c(1.73,&nbsp;1.65,&nbsp;2.02,&nbsp;1.89,&nbsp;2.61,&nbsp;1.36,&nbsp;2.37,&nbsp;2.08,&nbsp;2.69,&nbsp;
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.32,&nbsp;3.67)
&nbsp;
</pre> </font>

<div class="p"><!----></div>
(don't try to enter the <tt>+</tt>, which is a continuation character as
described above).
The function <tt>c()</tt> <em>combines</em> the individual numbers into 
a vector.  Try recalling (with 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mo stretchy="false">&uarr;</mo></mrow></math>) and modifying the above command to 

<pre>
Light=20,20,20,20,21,24,44,60,90,94,101

</pre> 
and see the error message you get: in order to create a vector of 
specified numbers,
you <b>must</b> use the <tt>c()</tt> function.

<div class="p"><!----></div>
To see a histogram of the growth rates enter <tt>&nbsp;&gt;&nbsp;hist(rmax)&nbsp;</tt>,
which opens a graphics window and displays the histogram. There are
<b>many</b> other built-in statistics functions: for example
<tt>mean(rmax)</tt> gets you the mean, and <tt>sd(rmax)</tt> and
<tt>var(rmax)</tt> give the standard deviation and variance,
respectively.  Play around with these functions, and any others you
can think of.

<div class="p"><!----></div>
To see how the algal rate of increase is affected by light intensity, type

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;plot(Light,&nbsp;rmax)
&nbsp;
</pre> </font>

<div class="p"><!----></div>
to plot <tt>rmax</tt> (
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>y</mi></mrow></math>) against <tt>Light</tt> (
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>x</mi></mrow></math>).
A linear regression seems reasonable. <b>Don't close this plot
window:</b> we'll soon be adding to it.

<div class="p"><!----></div>
To perform linear regression we create a linear model using the <tt>lm()</tt> 
(<b>l</b>inear <b>m</b>odel) function:

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;fit&nbsp;=&nbsp;lm(rmax&nbsp;~&nbsp;Light)
&nbsp;
</pre> </font>

<div class="p"><!----></div>
(Note that the variables are in the <em>opposite order</em> from the
<tt>plot()</tt> command, which is <tt>plot(x,y)</tt>, whereas the
linear model is read as "model 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msub><mrow><mi>r</mi></mrow><mrow>
<mtext>max</mtext>
</mrow>
</msub>
</mrow></math> as a function of light".)

<div class="p"><!----></div>
The <tt>lm</tt> command produces no output whatsoever, but it has created <tt>fit</tt>  
as an <b>object</b>, i.e. a data structure consisting of multiple parts,
holding the results of a regression analysis with <tt>rmax</tt> being modeled as a
function of <tt>Light</tt>. Unlike most statistics packages,  R&nbsp;rarely produces automatic 
summary output from an analysis. Statistical analyses in  R&nbsp;are done by creating a 
model, and then giving additional commands to extract desired information 
about the model or display results graphically.

<div class="p"><!----></div>
To get a summary of the results, enter 
the command <tt>&nbsp;&gt;&nbsp;summary(fit)&nbsp;</tt>.  R&nbsp;sets up model objects (more on this later) 
so that the function <tt>summary()</tt> 
"knows" that <tt>fit</tt> was created by <tt>lm()</tt>, and produces an appropriate
summary of results for an <tt>lm()</tt> object: 

<div class="p"><!----></div>
   <font color="#FF0000">
<pre>
&#62;&nbsp;summary(fit)
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>

Call:
lm(formula&nbsp;=&nbsp;rmax&nbsp;~&nbsp;Light)

Residuals:
&nbsp;&nbsp;&nbsp;&nbsp;Min&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1Q&nbsp;&nbsp;Median&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3Q&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Max&nbsp;
-0.5478&nbsp;-0.2607&nbsp;-0.1166&nbsp;&nbsp;0.1783&nbsp;&nbsp;0.7431&nbsp;

Coefficients:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Estimate&nbsp;Std.&nbsp;Error&nbsp;t&nbsp;value&nbsp;Pr(&#62;|t|)&nbsp;&nbsp;&nbsp;&nbsp;
(Intercept)&nbsp;1.580952&nbsp;&nbsp;&nbsp;0.244519&nbsp;&nbsp;&nbsp;6.466&nbsp;0.000116&nbsp;***
Light&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.013618&nbsp;&nbsp;&nbsp;0.004317&nbsp;&nbsp;&nbsp;3.154&nbsp;0.011654&nbsp;*&nbsp;&nbsp;
---
Signif.&nbsp;codes:&nbsp;&nbsp;0&nbsp;'***'&nbsp;0.001&nbsp;'**'&nbsp;0.01&nbsp;'*'&nbsp;0.05&nbsp;'.'&nbsp;0.1&nbsp;'&nbsp;'&nbsp;1&nbsp;

Residual&nbsp;standard&nbsp;error:&nbsp;0.4583&nbsp;on&nbsp;9&nbsp;degrees&nbsp;of&nbsp;freedom
Multiple&nbsp;R-Squared:&nbsp;0.5251,	Adjusted&nbsp;R-squared:&nbsp;0.4723&nbsp;
F-statistic:&nbsp;9.951&nbsp;on&nbsp;1&nbsp;and&nbsp;9&nbsp;DF,&nbsp;&nbsp;p-value:&nbsp;0.01165&nbsp;


&nbsp;
</pre> </font>

<div class="p"><!----></div>
[If you've had (and remember) a statistics course the output will make sense to you. 
The table of coefficients gives the estimated regression
line as 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msub><mrow><mi>r</mi></mrow><mrow>
<mtext>max</mtext>
</mrow>
</msub>
<mo>=</mo><mn>1</mn><mo>.</mo><mn>58</mn><mo>+</mo><mn>0</mn><mo>.</mo><mn>0136</mn><mo>&times;</mo><mi>Light</mi></mrow></math>, and associated with
each coefficient is the standard error of the estimate, the 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>t</mi></mrow></math>-statistic
value for testing whether the coefficient is nonzero, and
the 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>p</mi></mrow></math>-value corresponding to the 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>t</mi></mrow></math>-statistic. Below the table,
the adjusted R-squared gives the estimated fraction of the variance
explained by the regression line, and the 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>p</mi></mrow></math>-value in the last line
is an overall test for significance of the model against the null
hypothesis that the response variable is independent of the predictors.]

<div class="p"><!----></div>
You can add the regression line to the plot of the data with
a function taking <tt>fit</tt> as its input
(if you closed the plot of the data, you will need to create it again
in order to add the regression line):

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;abline(fit)
&nbsp;
</pre> </font>

<div class="p"><!----></div>
(<tt>abline</tt>, pronounced "a b line", is a general-purpose function
for adding lines to a plot: you can specify horizontal or vertical lines,
a slope and an intercept, or a regression model: <tt>?abline</tt>). 

<div class="p"><!----></div>

<div class="p"><!----></div>
<a name="tth_fIg1">
</a> 
<center><img src="lab1-014.png" alt="lab1-014.png" />
</center>
<a name="Intro1.Fig1">
</a>

<center>Figure 1: Graphical summary of regression analysis</center>

<div class="p"><!----></div>
You can get the coefficients by using the <tt>coef()</tt> function:

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;coef(fit)
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
(Intercept)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Light&nbsp;
&nbsp;1.58095214&nbsp;&nbsp;0.01361776&nbsp;

&nbsp;
</pre> </font>

<div class="p"><!----></div>
You can also also "interrogate" <tt>fit</tt> directly. Type 
<tt>&nbsp;&gt;&nbsp;names(fit)&nbsp;</tt> to get a list of the components of <tt>fit</tt>,
and then extract components according to their names
using the <tt>$</tt> symbol.

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;names(fit)
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
&nbsp;[1]&nbsp;"coefficients"&nbsp;&nbsp;"residuals"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"effects"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"rank"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;[5]&nbsp;"fitted.values"&nbsp;"assign"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"qr"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"df.residual"&nbsp;&nbsp;
&nbsp;[9]&nbsp;"xlevels"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"call"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"terms"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"model"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;
</pre> </font>

<div class="p"><!----></div>
For more information (perhaps more than you want)
about <tt>fit</tt>, use <tt>str(fit)</tt> (for <b>str</b>ucture).
You can get the regression coefficients this way:

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;fit$coefficients
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
(Intercept)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Light&nbsp;
&nbsp;1.58095214&nbsp;&nbsp;0.01361776&nbsp;

&nbsp;
</pre> </font>

<div class="p"><!----></div>
It's good to be able to look inside  R&nbsp;objects
when necessary, but all other things being
equal you should prefer (e.g.) <tt>coef(x)</tt>
to <tt>x$coefficients</tt>.

<div class="p"><!----></div>
 <h2><a name="tth_sEc4">
4</a>&nbsp;&nbsp;Script files and data files</h2>
Modeling and complicated data analysis are often accomplished more efficiently
using <em>scripts</em>, which are a series of 
commands stored in a text file. The Windows and MacOS
versions of  R&nbsp;both have basic script editors:
you can also use Windows Notepad or Wordpad,
or a more featureful editor like PFE, Xemacs, or Tinn-R:
you <b>shouldn't</b> use MS Word - see below ....

<div class="p"><!----></div>
Most programs for working with models or analyzing data follow a
simple pattern of program parts:

<ol type="1">
<li> "Setup" statements.
<div class="p"><!----></div>
</li>

<li> Input some data from a file or the keyboard.
<div class="p"><!----></div>
</li>

<li> Carry out the calculations that you want.
<div class="p"><!----></div>
</li>

<li> Print the results, graph them, or save them to a file.
<div class="p"><!----></div>
</li>
</ol>

<div class="p"><!----></div>
For example, a script file might

<ol type="1">
<li> Load some packages, or run another script file that 
creates some functions (more on functions later).
<div class="p"><!----></div>
</li>

<li> Read in data from a text file.
<div class="p"><!----></div>
</li>

<li> Fit several statistical models to the data and
compare them.
<div class="p"><!----></div>
</li>

<li> Graph the results, and save the graph to disk for including
in your term project.
<div class="p"><!----></div>
</li>
</ol>
Even for relatively simple tasks, script files are useful for build up a 
calculation step-by-step, making sure that each part works before adding on to it.

<div class="p"><!----></div>
Tips for working with data and script files
(sounding slightly scary but just trying to help you avoid common
pitfalls):

<ul>
<li>To let  R&nbsp;know where data and script files are located, you have three choices:

<ol type="1">
<li>spell out the <em>path</em>, or file location, explicitly.  &nbsp;There are two different ways to
specify paths: a single forward slash
(e.g. <tt>"c:/My&nbsp;Documents/R/script.R"</tt>)
or a double backslash (e.g. <tt>"c:\\My&nbsp;Documents\\R\\forest.txt"</tt>).
 R&nbsp;understands either of these, although you might as 
well just use the single forward slash, which works on all
platforms. 
<div class="p"><!----></div>
</li>

<li>change your working directory to wherever the file(s) are located
using <tt>Change dir</tt> in the <tt>File</tt> menu;
<div class="p"><!----></div>
</li>

<li>change your working directory to wherever the file(s) are located
using the <tt>setwd()</tt> (<b>set</b> <b>w</b>orking <b>d</b>irectory)
function, e.g. <tt>setwd("c:/temp")</tt>
<div class="p"><!----></div>
</li>
</ol>
Changing your working directory may be more efficient
in the long run, if you save all the
script and data files for a particular project in the same directory
and switch to that directory when you start work.

<div class="p"><!----></div>
If you have a shortcut defined for  R&nbsp;on your desktop
(or possibly ?? in the Start menu)
you can <em>permanently</em> change your default working directory
by right-clicking on the shortcut icon, selecting
<tt>Properties</tt>, and changing the starting directory to somewhere
like (for example) <tt>My Documents/R work</tt>.

<div class="p"><!----></div>
</li>

<li>it's important that data and script files be preserved as
<em>plain text</em> (or sometimes comma-separated) files.  There are
three things that can go wrong here: (1) if you use a web browser to
download files, be careful that it doesn't automatically append some
weird suffix to the files; (2) if your web browser has a "file association"
(e.g. it thinks that all files ending in <tt>.dat</tt> are Excel files),
make sure to save the file as plain text, and without
any extra extensions; (3) <b>never
use Microsoft Word to edit your data and script files</b>; MS Word will
try very hard to get you to save them as Word (rather than text) files,
which will screw them up!
<div class="p"><!----></div>
</li>

<li>If you send script files by e-mail, even if you paste them into
the message as plain text, lines will occasionally get broken in
different places - leading to confusion.  Beware.
<div class="p"><!----></div>
</li>
</ul>

<div class="p"><!----></div>
As a first example, the file <tt>Intro1.R</tt> has the commands from the interactive 
regression analysis. <b>Important:</b> 
before working with an example file, create  
a personal copy in some location on your own computer. We will refer to this location
as your <em>temp folder</em>. At the end of a lab session 
you <b>must</b> move files onto your personal disk (or email them to yourself).  

<div class="p"><!----></div>
Now open <b>your copy</b> of <tt>Intro1.R</tt>. In your editor, select 
and Copy the entire text of the file, and then Paste the text into the
 R&nbsp;console window (<tt>Ctrl-C</tt> and <tt>Ctrl-V</tt> as shortcuts). 
This has the same effect as entering the commands
by hand into the console: they will be executed and so a graph is 
displayed with the results. Cut-and-Paste allows you to execute script
files one piece at a time (which is useful for finding and fixing errors). The
<tt>source</tt> function allows you to run an entire script file, e.g. 

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;source("c:/temp/Intro1.R")
&nbsp;
</pre> </font>

<div class="p"><!----></div>
<tt>source()</tt>ing can also be done by pointing and clicking
via the <b>File</b> menu on the console window. 

<div class="p"><!----></div>
Another important time-saver is loading data from a text file. Grab  
copies of <tt>Intro2.R</tt> and <tt>ChlorellaGrowth.txt</tt> from the 
web page to see how this is done. In <tt>ChlorellaGrowth.txt</tt>
the two variables are entered as columns of a data matrix. Then 
instead of typing these in by hand, the command

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;X&nbsp;=&nbsp;read.table("ChlorellaGrowth.txt")
&nbsp;
</pre> </font>

<div class="p"><!----></div>
reads the file (from the current directory) 
and puts the data values into the variable 
<tt>X</tt>. <b>Note</b> that as specified above
you need to make sure that  R&nbsp;is looking 
for the data file in the right place ...
either move the data file to your current
working directory, or change the line so
that it points to the actual location
of the data file.

<div class="p"><!----></div>
Extract the variables from <tt>X</tt> with the commands

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;Light&nbsp;=&nbsp;X[,&nbsp;1]
&#62;&nbsp;rmax&nbsp;=&nbsp;X[,&nbsp;2]
&nbsp;
</pre> </font>

<div class="p"><!----></div>
Think of these as shorthand for "<tt>Light</tt> = everything in column 1 of <tt>X</tt>", and 
"<tt>rmax</tt> = everything in column 2 of <tt>X</tt>" (we'll learn about working with 
data matrices later). From there on out it's the same as before, with some additions
that set the axis labels and add a title.  

<div class="p"><!----></div>
<b>Exercise 3</b> Make a copy of <b>Intro2.R</b> under a new 
name, and modify the copy so that it does linear regression  
of algal growth rate on the natural log of light intensity, 
<tt>LogLight=log(Light)</tt>, and plots the data appropriately. You should end up with 
a graph that resembles Figure <a href="#Intro1Fig2">2</a>. 

<div class="p"><!----></div>

<div class="p"><!----></div>
<a name="tth_fIg2">
</a> <img src="lab1-021.png" alt="lab1-021.png" />

<div class="p"><!----></div>
<center>Figure 2: Graphical summary of regression analysis using log of light intensity
(and annotating the plot)</center>
<a name="Intro1Fig2">
</a>

<div class="p"><!----></div>
<b>Exercise 4</b> Run <b>Intro2.R</b>, then enter the command 
<tt>plot(fit)</tt> in the console and follow the directions in the console.
Figure out what just happened by entering <tt>?plot.lm</tt> to
bring up the Help page for the function <tt>plot.lm()</tt> that carries out a 
<tt>plot()</tt> command for an object produced by <tt>lm()</tt>. 
(This is one example of how  R&nbsp;uses the fact that statistical analyses are
stored as model objects. <tt>fit</tt> "knows" what kind of object it is 
(in this case an object of type <tt>lm</tt>), and 
so <tt>plot(fit)</tt> invokes a function that produces plots suitable 
for an <tt>lm</tt> object.) <b>Answer:</b>  R&nbsp;produced a series of 
diagnostic plots exploring whether or not the fitted linear model is a 
suitable fit to the data. In each of the plots, the 3 most extreme points
(the most likely candidates for "outliers") have been identified 
according to their sequence in the data set. 

<div class="p"><!----></div>
<b>Exercise 5</b> The axes in plots are scaled automatically, but the outcome is
not always ideal (e.g. if you want several graphs with exactly the
same axes limits). You can control scaling using the <tt>xlim</tt> and <tt>ylim</tt>
arguments in <tt>plot</tt>: <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <tt>plot(x,y,xlim=c(x1,x2), [other stuff]) </tt> <br />
will draw the graph with the 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>x</mi></mrow></math>-axis running from <tt>x1</tt> to <tt>x2</tt>, and using 
<tt>ylim=c(y1,y2)</tt> within the <tt>plot()</tt> command will 
do the same for the 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>y</mi></mrow></math>-axis. 

<div class="p"><!----></div>
Create a plot of growth rate versus light intensity with the

<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>x</mi></mrow></math>-axis running from 0 to 120, and the 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>y</mi></mrow></math>-axis running from 1 to 4. 

<div class="p"><!----></div>
<b>Exercise 6</b> Several graphs can be placed  
within a single figure by using the <tt>par</tt> 
function (short for "parameter") to adjust the layout of the plot. For example the 
command <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <tt>par(mfrow=c(m,n))</tt> <br />
divides the plotting area into 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>m</mi></mrow></math> rows and 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>n</mi></mrow></math> columns. As a series of graphs is drawn,
they are placed along the top row from left to right, then along the next row, and so on.
<tt>mfcol=c(m,n)</tt> has the same effect except that successive graphs are drawn
down the first column, then down the second column, and so on. 

<div class="p"><!----></div>
Save <b>Intro2.R</b> with a new name and modify the program as
follows. Use <tt>mfcol=c(2,1)</tt> to create graphs of growth rate as a 
function of <tt>Light</tt>, and of log(growth rate) as a function of 
<tt>log(Light)</tt> in the same figure.  
Do the same again, using <tt>mfcol=c(1,2)</tt>. 

<div class="p"><!----></div>
<b>Exercise 7*</b> Use <tt>?par</tt> to read about other plot control parameters
that can be set using <tt>par()</tt> (feel free to just skim - this is one of the
longest help files in the whole  R&nbsp;system!). Then draw a 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mn>2</mn><mo>&times;</mo><mn>2</mn></mrow></math> set of plots,
each showing the line 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>y</mi><mo>=</mo><mn>5</mn><mi>x</mi><mo>+</mo><mn>3</mn></mrow></math> with x running from 3 to 8, but with 4 different
line styles and 4 different line colors. 

<div class="p"><!----></div>
<b>Exercise 8*</b> Modify one of your scripts so that
at the very end it saves the plot to disk. In Windows you can do
this with <tt>savePlot()</tt>, otherwise with <tt>dev.print()</tt>.
Use <tt>?savePlot</tt>, <tt>?dev.print</tt> to read about these functions.  
Note that the argument <tt>filename</tt> can include the 
path to a folder; for example, in Windows you can use 
<tt>filename="c:/temp/Intro2Figure".</tt>

<div class="p"><!----></div>
(These are really exercises in using the help system, with 
the bonus that you learn some things about <tt>plot()</tt>. (Let's see, 
we know <tt>plot()</tt> can graph data points (
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msub><mrow><mi>r</mi></mrow><mrow>
<mtext>max</mtext>
</mrow>
</msub>
</mrow></math> versus 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>Light</mi></mrow></math>
and all that). Maybe it can also draw a line to connect the points, or 
just draw the line and leave out the points. That would be useful. So
let's try <tt>?plot</tt> and see if it says anything about lines ... Hey, it
also says that <tt>graphical parameters can be given as arguments to plot</tt>,
so maybe I can set line colors inside the plot command instead of using
<tt>par</tt> all the time....) The help system can be quite helpful once
you get used to it and get in the habit of using it often.)

<div class="p"><!----></div>
Some more tips on the help system:

<ul>
<li><tt>help.start()</tt> fires up a web browser pointing at all of the
help files;
<div class="p"><!----></div>
</li>

<li><tt>help()</tt> or <tt>?</tt> only search through functions in
the <em>currently loaded</em> packages (we'll get there);
<tt>help.search</tt> looks through all of the <em>installed</em>
packages;
<div class="p"><!----></div>
</li>

<li>as mentioned above, <tt>apropos()</tt> just looks through all
accessible  R&nbsp;objects, which means it will match names of functions
containing a given string
<div class="p"><!----></div>
</li>

<li><tt>help.search</tt> uses "fuzzy matching" - for example, 
<tt>help.search("log")</tt> finds 528 entries (on my
particular system) including lots of
functions with "plot", which includes the letters "lot", which
are <em>almost</em> like "log".  If you can't stand it, you
can turn this behavior off by specifying the incantation
<tt>help.search("log",agrep=FALSE)</tt> (81 results which still include
matches for "logistic", "myelogenous", and "phylogeny" ...)
<div class="p"><!----></div>
</li>

<li><tt>help(package=pkg)</tt> gives information on all  the objects
in a particular package <tt>pkg</tt> (again, more about packages later)
<div class="p"><!----></div>
</li>

<li>if you're connected to the Web, you can use the <tt>RSiteSearch()</tt>
command (from the command line or the Help menu) to do a full-text
search of all  R&nbsp;documentation and the mailing list archives
<div class="p"><!----></div>
</li>

<li><tt>example(function)</tt> will run all of the examples in the
help page, if any, for function <tt>function</tt>
<div class="p"><!----></div>
</li>

<li><tt>demo(topic)</tt> runs demonstration code on topic <tt>topic</tt>: type
<tt>demo()</tt> by itself to list all available demos
<div class="p"><!----></div>
</li>
</ul>

<div class="p"><!----></div>

<div class="p"><!----></div>

<div class="p"><!----></div>
The main point is not to be afraid of experimenting; if you have saved your
previous commands in a script file, there's almost nothing you can break
by trying out commands and inspecting the results.

<div class="p"><!----></div>
 <h2><a name="tth_sEc5">
5</a>&nbsp;&nbsp;Statistics in  R</h2>
Some of the important functions and packages (collections
of functions) for statistical modeling and data analysis are summarized in Table 
<a href="#StatModelingFunctions">2</a>. The book <em>Modern Applied Statistics
with S</em> by Venables and Ripley [<a href="#MASS" name="CITEMASS">3</a>] gives a good practical overview, and a 
list of available packages and their contents can be found at the main  R&nbsp;website 
(<a href="http://www.cran.r-project.org"><tt>http://www.cran.r-project.org</tt></a>, and click on <tt>Package sources</tt>). For the most
part, we will not be concerned here with this side of  R.

<div class="p"><!----></div>

<div class="p"><!----></div>
<a name="tth_tAb2">
</a> 
<table>
<tr><td width="194"><tt>aov</tt>, <tt>anova</tt> </td><td width="402">Analysis of variance or deviance</td></tr>
<tr><td width="194"><tt>lm</tt> </td><td width="402">Linear models (regression, ANOVA, ANCOVA) </td></tr>
<tr><td width="194"><tt>glm</tt> </td><td width="402">Generalized linear models (e.g. logistic, Poisson regression) </td></tr>
<tr><td width="194"><tt>gam</tt> </td><td width="402">Generalized additive models (in package <tt>mgcv</tt>) </td></tr>
<tr><td width="194"><tt>nls</tt> </td><td width="402">Fit nonlinear models by least-squares</td></tr>
<tr><td width="194"><tt>lme</tt>, <tt>nlme</tt> </td><td width="402">Linear and nonlinear mixed-effects models (repeated
measures, block effects, spatial models): in package <tt>nlme</tt> </td></tr>
<tr><td width="194"><tt>boot</tt> </td><td width="402">Package: bootstrapping functions </td></tr>
<tr><td width="194"><tt>splines</tt>  </td><td width="402">Package: nonparametric regression (more in packages
        <tt>fields</tt>, <tt>KernSmooth</tt>, <tt>logspline</tt>, <tt>sm</tt> and others)</td></tr>
<tr><td width="194"><tt>princomp</tt>, <tt>manova</tt>, <tt>lda</tt>, <tt>cancor</tt>
 </td><td width="402">Multivariate analysis
(some in package <tt>MASS</tt>;
also see packages <tt>vegan</tt>, <tt>ade4</tt>) </td></tr>
<tr><td width="194"><tt>survival</tt> </td><td width="402">Package: survival analysis </td></tr>
<tr><td width="194"><tt>tree</tt>, <tt>rpart</tt> </td><td width="402">Packages: tree-based regression </td></tr></table>


<center>Table 2: A few of the functions and packages in  R&nbsp;for statistical
modeling and data analysis. There are <b>many</b> more, but you will have
to learn about them somewhere else. </center> 
<a name="StatModelingFunctions">
</a>

<div class="p"><!----></div>
 <h2><a name="tth_sEc6">
6</a>&nbsp;&nbsp;Vectors</h2> 
Vectors and matrices (1- and 2-dimensional rectangular arrays of 
numbers) are pre-defined data types in  R. Operations with vectors 
and matrices may seem a bit abstract now, but we need them to do 
useful things later. 
Vectors' only properties are their type (or <em>class</em>)
and length, although they can also have an
associated list of names.

<div class="p"><!----></div>
We've already seen two ways to create vectors in  R:

<ol type="1">
<li>A command in the console window or a script file listing the values, 
such as

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;initialsize&nbsp;=&nbsp;c(1,&nbsp;3,&nbsp;5,&nbsp;7,&nbsp;9,&nbsp;11)
&nbsp;
</pre> </font>

<div class="p"><!----></div>

<div class="p"><!----></div>
</li>

<li>Using <tt>read.table()</tt>:

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;initialsize&nbsp;=&nbsp;read.table("c:/temp/initialdata.txt")
&nbsp;
</pre> </font>

<div class="p"><!----></div>

<div class="p"><!----></div>
</li>
</ol>

<div class="p"><!----></div>
A vector can then be used in calculations as if it were 
a number (more or less) 

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;finalsize&nbsp;=&nbsp;initialsize&nbsp;+&nbsp;1
&#62;&nbsp;newsize&nbsp;=&nbsp;sqrt(initialsize)
&#62;&nbsp;finalsize
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;&nbsp;2&nbsp;&nbsp;4&nbsp;&nbsp;6&nbsp;&nbsp;8&nbsp;10&nbsp;12

&nbsp;
</pre> </font>
  <font color="#FF0000">
<pre>
&#62;&nbsp;newsize
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;1.000000&nbsp;1.732051&nbsp;2.236068&nbsp;2.645751&nbsp;3.000000&nbsp;3.316625

&nbsp;
</pre> </font>

<div class="p"><!----></div>
Notice that the operations were applied to every entry in the vector. 
Similarly, commands like <tt>initialsize-5, 2*initialsize, initialsize/10</tt> 
apply subtraction, multiplication, and division to each element of the 
vector. The same is true for 

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;initialsize^2
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;9&nbsp;&nbsp;25&nbsp;&nbsp;49&nbsp;&nbsp;81&nbsp;121

&nbsp;
</pre> </font>

<div class="p"><!----></div>
In  R&nbsp;the default is to apply functions and operations to vectors
in an <em>element by element</em> manner; anything else (e.g. matrix
multiplication) is done using special notation (discussed below). 

<div class="p"><!----></div>
     <h3><a name="tth_sEc6.1">
6.1</a>&nbsp;&nbsp;Functions for creating vectors</h3>
A set of regularly spaced values can be created with the <tt>seq</tt>
function, whose syntax is 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <tt>x=seq(from,to,by)</tt> or <tt>x=seq(from,to)</tt> or
<tt>x=seq(from,to,length.out)</tt><br />

<div class="p"><!----></div>
The first form generates a vector <tt>(from,from+by,from+2*by,...)</tt>
with the last entry not extending further than than <tt>to</tt>; in the second 
form the value of <tt>by</tt> is assumed to be 1 or -1, depending on
whether <tt>from</tt> or <tt>to</tt> is larger; and the third
form creates a vector with the desired endpoints and length.
There is also a shortcut for creating vectors with <tt>by=1</tt>:

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;1:8
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;1&nbsp;2&nbsp;3&nbsp;4&nbsp;5&nbsp;6&nbsp;7&nbsp;8

&nbsp;
</pre> </font>

<div class="p"><!----></div>
<b>Exercise 9</b> Use <tt>seq</tt> to create the vector <tt>v=(1 5 9 13)</tt>,
and to create a vector going from 1 to 5 in increments of 0.2 . 

<div class="p"><!----></div>
A constant vector such as <tt>(1 1 1 1)</tt> can be created with <tt>rep</tt>
function, whose basic syntax is <tt>&nbsp;rep(values,lengths)&nbsp;</tt>.  For example,

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;rep(3,&nbsp;5)
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;3&nbsp;3&nbsp;3&nbsp;3&nbsp;3

&nbsp;
</pre> </font>

<div class="p"><!----></div>
creates a vector in which the value 3 is repeated 5 times. 
<tt>rep()</tt> will repeat a whole vector multiple times

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;rep(1:3,&nbsp;3)
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;1&nbsp;2&nbsp;3&nbsp;1&nbsp;2&nbsp;3&nbsp;1&nbsp;2&nbsp;3

&nbsp;
</pre> </font>

<div class="p"><!----></div>
or will repeat each of the elements in a vector a given
number of times:

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;rep(1:3,&nbsp;each&nbsp;=&nbsp;3)
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;1&nbsp;1&nbsp;1&nbsp;2&nbsp;2&nbsp;2&nbsp;3&nbsp;3&nbsp;3

&nbsp;
</pre> </font>

<div class="p"><!----></div>
Even more flexibly, you can repeat each element in the vector
a different number of times:

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;rep(c(3,&nbsp;4),&nbsp;c(2,&nbsp;5))
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;3&nbsp;3&nbsp;4&nbsp;4&nbsp;4&nbsp;4&nbsp;4

&nbsp;
</pre> </font>

<div class="p"><!----></div>
The value 3 was repeated 2 times, followed by the value 4 repeated 5 times.
<tt>rep()</tt> can be a little bit mind-blowing as you get started, but you'll
get used to it - and it will turn out to be useful.

<div class="p"><!----></div>
Some of the main functions for creating and working with vectors are
listed in Table <a href="#VectorFunctions">3</a>. 

<div class="p"><!----></div>

<div class="p"><!----></div>
<a name="tth_tAb3">
</a> 
<table>
<tr><td width="194"><tt>seq(from,to,by=1)</tt> </td><td width="402">Vector of evenly spaced values, default increment = 1) </td></tr>
<tr><td width="194"><tt>seq(from, to, length.out)</tt> </td><td width="402">Vector of evenly spaced values, specified length) </td></tr>
<tr><td width="194"><tt>c(u,v,...) </tt> </td><td width="402">Combine a set of numbers and/or vectors into a single vector </td></tr>
<tr><td width="194"><tt>rep(a,b)</tt> </td><td width="402">Create vector by repeating elements of <tt>a</tt> by amounts in <tt>b</tt> </td></tr>
<tr><td width="194"><tt>as.vector(x)</tt> </td><td width="402">Convert an object of some other type to a vector </td></tr>
<tr><td width="194"><tt>hist(v)</tt> </td><td width="402">Histogram plot of value in v </td></tr>
<tr><td width="194"><tt>mean(v),var(v),sd(v)</tt> </td><td width="402">Estimate of population
mean, variance, standard deviation based on data values in <tt>v</tt> </td></tr>
<tr><td width="194"><tt>cor(v,w)</tt> </td><td width="402">Correlation between two vectors </td></tr></table>


<center>Table 3: Some important  R&nbsp;functions for creating and working with vectors. Many of these have other optional
arguments; use the help system (e.g. <tt>?cor</tt>) for more information. The statistical functions such as
<tt>var</tt> regard the values as samples from a population and compute an estimate of the population 
statistic; for example <tt>sd(1:3)=1</tt>.</center>
<a name="VectorFunctions">
</a>

<div class="p"><!----></div>
     <h3><a name="tth_sEc6.2">
6.2</a>&nbsp;&nbsp;Vector indexing</h3>
Often it is necessary to extract a specific 
entry or other part of a vector. This procedure
is called <em>vector indexing</em>, and uses
square brackets ([]):

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;z&nbsp;=&nbsp;c(1,&nbsp;3,&nbsp;5,&nbsp;7,&nbsp;9,&nbsp;11)
&#62;&nbsp;z[3]
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;5

&nbsp;
</pre> </font>

<div class="p"><!----></div>
(how would you use <tt>seq()</tt> to construct <tt>z</tt>?)
<tt>z[3]</tt> extracts the third item, or <em>element</em>, in the vector <tt>z</tt>. 
You can also access a block of elements using the functions for 
vector construction, e.g. 

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;v&nbsp;=&nbsp;z[2:5]
&#62;&nbsp;v
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;3&nbsp;5&nbsp;7&nbsp;9

&nbsp;
</pre> </font>

<div class="p"><!----></div>
This has extracted the 2 through 5 elements in the vector. 
If you enter <tt>v=z[seq(1,5,2)]</tt>, what will happen? Try it
and see, and make sure you understand what happened. 

<div class="p"><!----></div>
Extracted parts of a vector don't have to be regularly spaced. For example

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;v&nbsp;=&nbsp;z[c(1,&nbsp;2,&nbsp;5)]
&#62;&nbsp;v
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;1&nbsp;3&nbsp;9

&nbsp;
</pre> </font>

<div class="p"><!----></div>
Indexing is also used to <b>set specific values within a vector</b>. For 
example, 

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;z[1]&nbsp;=&nbsp;12
&nbsp;
</pre> </font>

<div class="p"><!----></div>
changes the value of the first entry in <tt>z</tt> while leaving 
all the rest alone, and 

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;z[c(1,&nbsp;3,&nbsp;5)]&nbsp;=&nbsp;c(22,&nbsp;33,&nbsp;44)
&nbsp;
</pre> </font>

<div class="p"><!----></div>
changes the 1, 3, and 5 values.  

<div class="p"><!----></div>
<b>Exercise 10</b> Write a <em>one-line</em> command to extract 
a vector consisting of the second, first, and third elements of <tt>z</tt> 
<em>in that order</em>. 

<div class="p"><!----></div>

<div class="p"><!----></div>
<b>Exercise 11</b> Write a script file that computes values of 

<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>y</mi><mo>=</mo>
<mfrac><mrow><mo stretchy="false">(</mo><mi>x</mi><mo>-</mo><mn>1</mn><mo stretchy="false">)</mo></mrow>
<mrow><mo stretchy="false">(</mo><mi>x</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo></mrow>
</mfrac>
</mrow></math> for 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>x</mi><mo>=</mo><mn>1</mn><mo>,</mo><mn>2</mn><mo>,</mo><mo>&#x2026;</mo><mo>,</mo><mn>10</mn></mrow></math>, and plots 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>y</mi></mrow></math> versus 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>x</mi></mrow></math>
with the points plotted and connected by a line. 

<div class="p"><!----></div>
<b>Exercise 12</b> The sum of the geometric series 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mn>1</mn><mo>+</mo><mi>r</mi><mo>+</mo>
<msup><mrow><mi>r</mi></mrow><mrow><mn>2</mn></mrow>
</msup>
<mo>+</mo>
<msup><mrow><mi>r</mi></mrow><mrow><mn>3</mn></mrow>
</msup>
<mo>+</mo><mo>.</mo><mo>.</mo><mo>.</mo><mo>+</mo>
<msup><mrow><mi>r</mi></mrow><mrow><mi>n</mi></mrow>
</msup>
</mrow></math> 
approaches the limit 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mn>1</mn><mo stretchy="false">/</mo><mo stretchy="false">(</mo><mn>1</mn><mo>-</mo><mi>r</mi><mo stretchy="false">)</mo></mrow></math> for 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>r</mi><mo>&lt;</mo><mn>1</mn></mrow></math> as 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>n</mi><mo>&rarr;</mo><mi>&infin;</mi></mrow></math>.   
Take 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>r</mi><mo>=</mo><mn>0</mn><mo>.</mo><mn>5</mn></mrow></math> and 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>n</mi><mo>=</mo><mn>10</mn></mrow></math>, and write a <b>one-statement</b> command that creates 
the vector 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>G</mi><mo>=</mo><mo stretchy="false">(</mo>
<msup><mrow><mi>r</mi></mrow><mrow><mn>0</mn></mrow>
</msup>
<mo>,</mo>
<msup><mrow><mi>r</mi></mrow><mrow><mn>1</mn></mrow>
</msup>
<mo>,</mo>
<msup><mrow><mi>r</mi></mrow><mrow><mn>2</mn></mrow>
</msup>
<mo>,</mo><mo>.</mo><mo>.</mo><mo>.</mo><mo>,</mo>
<msup><mrow><mi>r</mi></mrow><mrow><mi>n</mi></mrow>
</msup>
<mo stretchy="false">)</mo></mrow></math>. Compare the sum (using <tt>sum()</tt>)
of this vector to the limiting value 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mn>1</mn><mo stretchy="false">/</mo><mo stretchy="false">(</mo><mn>1</mn><mo>-</mo><mi>r</mi><mo stretchy="false">)</mo></mrow></math>. Repeat for  

<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>n</mi><mo>=</mo><mn>50</mn></mrow></math>.

<div class="p"><!----></div>
     <h3><a name="tth_sEc6.3">
6.3</a>&nbsp;&nbsp;Logical operators</h3>
These operators return a logical value of <tt>TRUE</tt> or <tt>FALSE</tt>. For example, 
try:  

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;a&nbsp;=&nbsp;1
&#62;&nbsp;b&nbsp;=&nbsp;3
&#62;&nbsp;c&nbsp;=&nbsp;a&nbsp;&lt;&nbsp;b
&#62;&nbsp;d&nbsp;=&nbsp;(a&nbsp;&#62;&nbsp;b)
&#62;&nbsp;c
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;TRUE

&nbsp;
</pre> </font>
  <font color="#FF0000">
<pre>
&#62;&nbsp;d
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;FALSE

&nbsp;
</pre> </font>

<div class="p"><!----></div>
The parentheses around (<tt>a&gt;b</tt>) are optional but can be used to improve 
readability. One special case where you <em>do</em> need parentheses
(or spaces) is comparing to negative values;
<tt>a&lt;-1</tt> will surprise you by assigning the value 1 to <tt>a</tt>,
because <tt>&lt;-</tt> (representing a left-pointing arrow) is
an alternative way of assigning a value in  R.  Use 
<tt>a&lt;&nbsp;-1</tt>, or more safely <tt>a&lt;(-1)</tt>,
to make this comparison.

<div class="p"><!----></div>

<div class="p"><!----></div>
<a name="tth_tAb4">
</a>  [t]

<table>
<tr><td width="166">x 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mo>&lt;</mo></mrow></math> y  </td><td width="277">less than    </td></tr>
<tr><td width="166">x 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mo>&gt;</mo></mrow></math> y  </td><td width="277">greater than </td></tr>
<tr><td width="166">x 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mo>&lt;</mo><mo>=</mo></mrow></math> y </td><td width="277">less than or equal to </td></tr>
<tr><td width="166">x 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mo>&gt;</mo><mo>=</mo></mrow></math> y </td><td width="277">greater than or equal to </td></tr>
<tr><td width="166">x 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mo>=</mo><mo>=</mo></mrow></math> y </td><td width="277">equal to </td></tr></table>


<center>Table 4: Some comparison operators in  R. Use <tt>?Comparison</tt> to learn more.</center>
<a name="Comparisons">
</a>

<div class="p"><!----></div>
When we compare two vectors or matrices of the same size, or compare a 
number with a vector or matrix, comparisons are done element-by-element.  
For example,

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;x&nbsp;=&nbsp;1:5
&#62;&nbsp;b&nbsp;=&nbsp;(x&nbsp;&lt;=&nbsp;3)
&#62;&nbsp;b
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;&nbsp;TRUE&nbsp;&nbsp;TRUE&nbsp;&nbsp;TRUE&nbsp;FALSE&nbsp;FALSE

&nbsp;
</pre> </font>

<div class="p"><!----></div>
So if <tt>x</tt> and <tt>y</tt> are vectors, then <tt>(x==y)</tt> will return a vector of 
values giving the element-by-element comparisons. If you want to know
whether <tt>x</tt> and <tt>y</tt> are identical vectors, use <tt>identical(x,y)</tt>
which returns a single value: <tt>TRUE</tt> if each entry in <tt>x</tt> equals the
corresponding entry in <tt>y</tt>, otherwise <tt>FALSE</tt>. You can use <tt>?Logical</tt> to
read more about logical operators. 
<b>Note the difference between = and ==: can you
figure out what happened in the following cautionary tale?</b>

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;a&nbsp;=&nbsp;1:3
&#62;&nbsp;b&nbsp;=&nbsp;2:4
&#62;&nbsp;a&nbsp;==&nbsp;b
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;FALSE&nbsp;FALSE&nbsp;FALSE

&nbsp;
</pre> </font>
  <font color="#FF0000">
<pre>
&#62;&nbsp;a&nbsp;=&nbsp;b
&#62;&nbsp;a&nbsp;==&nbsp;b
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;TRUE&nbsp;TRUE&nbsp;TRUE

&nbsp;
</pre> </font>

<div class="p"><!----></div>

<div class="p"><!----></div>
 R&nbsp;also does arithmetic on logical values, treating <tt>TRUE</tt> as 1 and 
<tt>FALSE</tt> as 0. So <tt>&nbsp;sum(b)&nbsp;</tt> returns the value 3, telling us that 3 
entries of <tt>x</tt> satisfied the condition (<tt>x&lt;=3</tt>). This is useful for
(e.g.) seeing how many of the elements of a vector are larger than a cutoff
value.

<div class="p"><!----></div>
More complicated conditions are built by using <b>logical operators</b> to 
combine comparisons: 

<div class="p"><!----></div>

<table>
<tr><td align="center"><tt>!</tt> </td><td align="left">Negation  </td></tr>
<tr><td align="center"><tt>&amp;</tt>, <tt>&amp;&amp;</tt> </td><td align="left">AND  </td></tr>
<tr><td align="center"><tt>|</tt>, <tt>||</tt> </td><td align="left">OR 
</td></tr></table>


<div class="p"><!----></div>
OR is <em>non-exclusive</em>, meaning that <tt>x - y</tt> is true 
if either <tt>x</tt> or <tt>y</tt> or both are true. 
For example, try

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;a&nbsp;=&nbsp;c(1,&nbsp;2,&nbsp;3,&nbsp;4)
&#62;&nbsp;b&nbsp;=&nbsp;c(1,&nbsp;1,&nbsp;5,&nbsp;5)
&#62;&nbsp;(a&nbsp;&lt;&nbsp;b)&nbsp;&amp;&nbsp;(a&nbsp;&#62;&nbsp;3)
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;FALSE&nbsp;FALSE&nbsp;FALSE&nbsp;&nbsp;TRUE

&nbsp;
</pre> </font>
  <font color="#FF0000">
<pre>
&#62;&nbsp;(a&nbsp;&lt;&nbsp;b)&nbsp;|&nbsp;(a&nbsp;&#62;&nbsp;3)
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;FALSE&nbsp;FALSE&nbsp;&nbsp;TRUE&nbsp;&nbsp;TRUE

&nbsp;
</pre> </font>

<div class="p"><!----></div>
and make sure you understand what happened. The two forms of AND
and OR differ in how they handle vectors. The shorter one does
element-by-element comparisons; the longer one only looks at the
first element in each vector. 

<div class="p"><!----></div>
     <h3><a name="tth_sEc6.4">
6.4</a>&nbsp;&nbsp;Vector indexing II</h3> 
We can also use <em>logical</em> vectors (lists of <tt>TRUE</tt> and
<tt>FALSE</tt> values) to pick elements out of vectors.
This is important, e.g., for subsetting data (getting rid of those
pesky outliers!)

<div class="p"><!----></div>
As a simple example, we might want to focus on just the low-light
values of 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msub><mrow><mi>r</mi></mrow><mrow>
<mtext>max</mtext>
</mrow>
</msub>
</mrow></math> in the <em>Chlorella</em> example:

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;X&nbsp;=&nbsp;read.table("ChlorellaGrowth.txt")
&#62;&nbsp;Light&nbsp;=&nbsp;X[,&nbsp;1]
&#62;&nbsp;rmax&nbsp;=&nbsp;X[,&nbsp;2]
&#62;&nbsp;lowLight&nbsp;=&nbsp;Light[Light&nbsp;&lt;&nbsp;50]
&#62;&nbsp;lowLightrmax&nbsp;=&nbsp;rmax[Light&nbsp;&lt;&nbsp;50]
&#62;&nbsp;lowLight
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;20&nbsp;20&nbsp;20&nbsp;20&nbsp;21&nbsp;24&nbsp;44

&nbsp;
</pre> </font>
  <font color="#FF0000">
<pre>
&#62;&nbsp;lowLightrmax
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;1.73&nbsp;1.65&nbsp;2.02&nbsp;1.89&nbsp;2.61&nbsp;1.36&nbsp;2.37

&nbsp;
</pre> </font>

<div class="p"><!----></div>
What is really happening here (think about it
for a minute) is that <tt>Light&lt;50</tt> 
generates a logical vector the same
length as <tt>Light</tt> (<tt>TRUE TRUE TRUE ... </tt>)
which is then used to select the appropriate
values.

<div class="p"><!----></div>
If you want the positions at which <tt>Light</tt>
is lower than 50, you could say
<tt>(1:length(Light))[Light&lt;50]</tt>, but you
can also use a built-in function:
<tt>which(Light&lt;50)</tt>.  If you wanted
the position at which the maximum value
of <tt>Light</tt> occurs, you could say
<tt>which(Light==max(Light))</tt>.  (This
normally results in a vector of length 1; when
could it give a longer vector?)  There
is even a built-in command for this
specific function, <tt>which.max()</tt> (although
<tt>which.max()</tt> always returns just the
<em>first</em> position at which the maximum
occurs).

<div class="p"><!----></div>
(What would happen if instead of setting
<tt>lowLight</tt> you replaced <tt>Light</tt>
by saying <tt>Light=Light[Light&lt;50]</tt>?
Why would that be the wrong thing to do?)

<div class="p"><!----></div>
We can also combine logical operators (making sure to
use the element-by-element <tt>&amp;</tt> and <tt>|</tt> versions
of AND and OR):

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;Light[Light&nbsp;&lt;&nbsp;50&nbsp;&amp;&nbsp;rmax&nbsp;&lt;=&nbsp;2]
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;20&nbsp;20&nbsp;20&nbsp;24

&nbsp;
</pre> </font>
  <font color="#FF0000">
<pre>
&#62;&nbsp;rmax[Light&nbsp;&lt;&nbsp;50&nbsp;&amp;&nbsp;rmax&nbsp;&lt;=&nbsp;2]
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;1.73&nbsp;1.65&nbsp;1.89&nbsp;1.36

&nbsp;
</pre> </font>

<div class="p"><!----></div>
There are a huge number of variations on this theme.

<div class="p"><!----></div>
<b>Exercise 13</b> 
<tt>runif(n)</tt> is a function (more on it soon) that
generates a vector of <tt>n</tt> random, uniformly distributed
numbers between 0 and 1.  Create a vector of 20 numbers,
then find the subset of those numbers that is less than the mean.

<div class="p"><!----></div>
<b>Exercise 14*</b> 
Find the <em>positions</em> 
of the elements that are less than the mean
of the vector you just created (e.g. if your vector were
<tt>(0.1&nbsp;0.9.&nbsp;0.7&nbsp;0.3)</tt> the answer
would be <tt>(1&nbsp;4)</tt>).

<div class="p"><!----></div>
As I mentioned in passing above, vectors can have
names associated with their elements: if they do,
you can also extract elements by name (use
<tt>names()</tt> to find out the names).

<div class="p"><!----></div>
   <font color="#FF0000">
<pre>
&#62;&nbsp;x&nbsp;=&nbsp;c(first&nbsp;=&nbsp;7,&nbsp;second&nbsp;=&nbsp;5,&nbsp;third&nbsp;=&nbsp;2)
&#62;&nbsp;names(x)
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;"first"&nbsp;&nbsp;"second"&nbsp;"third"&nbsp;

&nbsp;
</pre> </font>
  <font color="#FF0000">
<pre>
&#62;&nbsp;x["first"]
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
first&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;7&nbsp;

&nbsp;
</pre> </font>
  <font color="#FF0000">
<pre>
&#62;&nbsp;x[c("third",&nbsp;"first")]
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
third&nbsp;first&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7&nbsp;

&nbsp;
</pre> </font>

<div class="p"><!----></div>
Finally, it is sometimes handy to be able to drop a
particular set of elements, rather than taking a
particular set: you can do this with negative indices.
For example, <tt>x[-1]</tt> extracts all but the first
element of a vector.

<div class="p"><!----></div>
<b>Exercise 15*</b> 
Specify two ways to take only the odd elements
of a vector of arbitrary length.

<div class="p"><!----></div>
 <h2><a name="tth_sEc7">
7</a>&nbsp;&nbsp;Matrices</h2>

     <h3><a name="tth_sEc7.1">
7.1</a>&nbsp;&nbsp;Creating matrices</h3>
Like vectors, matrices can be created by reading in 
values from a data file using <tt>read.table</tt>.
Matrices of numbers can also be entered by creating a vector of the matrix 
entries, and then reshaping them to the desired number of rows
and columns using the function <tt>matrix</tt>. For example 

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;X&nbsp;=&nbsp;matrix(c(1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;6),&nbsp;nrow&nbsp;=&nbsp;2,&nbsp;ncol&nbsp;=&nbsp;3)
&nbsp;
</pre> </font>

<div class="p"><!----></div>
takes the values 1 to 6 and reshapes them into a 2 by 3 matrix. 
Note that values in the data vector are put into the matrix 
<em>column-wise</em>, by default. You can change this by using the optional
parameter <tt>byrow</tt>. For example 

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;A&nbsp;=&nbsp;matrix(1:9,&nbsp;nrow&nbsp;=&nbsp;3,&nbsp;ncol&nbsp;=&nbsp;3,&nbsp;byrow&nbsp;=&nbsp;TRUE)
&#62;&nbsp;A
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,1]&nbsp;[,2]&nbsp;[,3]
[1,]&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;3
[2,]&nbsp;&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;&nbsp;6
[3,]&nbsp;&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp;9

&nbsp;
</pre> </font>

<div class="p"><!----></div>
 R&nbsp;will re-cycle through entries in the data vector, if need be,
to fill a matrix of the specified size. So for example <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <tt>matrix(1,nrow=50,ncol=50)</tt> <br />
creates a 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mn>50</mn><mo>&times;</mo><mn>50</mn></mrow></math> matrix of all 1's.

<div class="p"><!----></div>
<b>Exercise 16</b> Use a command of the form <tt>X=matrix(v,nrow=2,ncol=4)</tt>
where <tt>v</tt> is a data vector, to create the following matrix <tt>X</tt>:

<pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,1]&nbsp;[,2]&nbsp;[,3]&nbsp;[,4]
[1,]&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;1
[2,]&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;2

</pre>

<div class="p"><!----></div>
 R&nbsp;will also collapse a matrix to behave like a vector
whenever it makes sense: for example <tt>sum(X)</tt> above
is 12.

<div class="p"><!----></div>
<b>Exercise 17</b> Use <tt>rnorm</tt> and <tt>matrix</tt> to 
create a 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mn>5</mn><mo>&times;</mo><mn>7</mn></mrow></math> matrix of Gaussian random numbers with
mean 1 and standard deviation 2. 

<div class="p"><!----></div>
Another useful function for creating matrices is <tt>diag</tt>.
<tt>diag(v,n)</tt> creates an 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>n</mi><mo>&times;</mo><mi>n</mi></mrow></math> matrix with data
vector 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>v</mi></mrow></math> on its diagonal. So for example <tt>diag(1,5)</tt>
creates the 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mn>5</mn><mo>&times;</mo><mn>5</mn></mrow></math> <em>identity matrix</em>, which has 1's on
the diagonal and 0 everywhere else.

<div class="p"><!----></div>
Finally, in Windows one can use the <tt>data.entry</tt> function. 
This function can only edit existing matrices, but for example (try this now!!)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <tt>A=matrix(0,3,4); data.entry(A)</tt> <br />
will create <tt>A</tt> as a 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mn>3</mn><mo>&times;</mo><mn>4</mn></mrow></math> matrix, and then call up 
a spreadsheet-like interface in which the values can be changed to
whatever you need.  

<div class="p"><!----></div>

<div class="p"><!----></div>
<a name="tth_tAb5">
</a> 
<table>
<tr><td width="201"><tt>matrix(v,nrow=m,ncol=n)</tt> </td><td width="402">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>m</mi><mo>&times;</mo><mi>n</mi></mrow></math> matrix using the values in <tt>v</tt> </td></tr>
<tr><td width="201"><tt>t(A)</tt> </td><td width="402">transpose (exchange rows and columns) of matrix <tt>A</tt> </td></tr>
<tr><td width="201"><tt>dim(X)</tt> </td><td width="402">dimensions of matrix X. <tt>dim(X)[1]</tt>=# rows, <tt>dim(X)[2]</tt>=# columns </td></tr>
<tr><td width="201"><tt>data.entry(A)</tt> </td><td width="402">call up a spreadsheet-like interface to edit the values in <tt>A</tt> </td></tr>
<tr><td width="201"><tt>diag(v,n)</tt> </td><td width="402">diagonal 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>n</mi><mo>&times;</mo><mi>n</mi></mrow></math> matrix with 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>v</mi></mrow></math> on diagonal, 0 elsewhere 
(<tt>v</tt> is 1 by default, so <tt>diag(n)</tt> gives an 
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mi>n</mi><mo>&times;</mo><mi>n</mi></mrow></math> identity matrix)</td></tr>
<tr><td width="201"><tt>cbind(a,b,c,...)</tt> </td><td width="402">combine compatible objects by attaching them along columns </td></tr>
<tr><td width="201"><tt>rbind(a,b,c,...)</tt> </td><td width="402">combine compatible objects by attaching them along rows </td></tr>
<tr><td width="201"><tt>as.matrix(x)</tt> </td><td width="402">convert an object of some other type to a matrix, if possible </td></tr>
<tr><td width="201"><tt>outer(v,w)</tt> </td><td width="402">"outer product" of vectors <tt>v</tt>, <tt>w</tt>: the matrix whose 

<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo></mrow></math>
element is <tt>v[i]*w[j]</tt> </td></tr></table>


<center>Table 5: Some important functions for creating and working with matrices. Many
of these have additional optional arguments; use the help system for full details.</center>
<a name="MatrixFunctions">
</a>

<div class="p"><!----></div>
     <h3><a name="tth_sEc7.2">
7.2</a>&nbsp;&nbsp;cbind and rbind</h3> 
If their sizes match, vectors can be combined to form matrices, and matrices
can be combined with vectors or matrices to form other matrices. The functions
that do this are <tt>cbind</tt> and <tt>rbind</tt>. 

<div class="p"><!----></div>
<tt>cbind</tt> binds together columns of two objects. One thing it can do
is put vectors together to form a matrix: 

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;C&nbsp;=&nbsp;cbind(1:3,&nbsp;4:6,&nbsp;5:7)
&#62;&nbsp;C
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,1]&nbsp;[,2]&nbsp;[,3]
[1,]&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;5
[2,]&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;&nbsp;6
[3,]&nbsp;&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp;7

&nbsp;
</pre> </font>

<div class="p"><!----></div>
Remember that  R&nbsp;interprets vectors as row or column vectors according to
what you're doing with them. Here it treats them as column vectors so that 
columns exist to be bound together. On the other hand, 

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;D&nbsp;=&nbsp;rbind(1:3,&nbsp;4:6)
&#62;&nbsp;D
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,1]&nbsp;[,2]&nbsp;[,3]
[1,]&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;3
[2,]&nbsp;&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;&nbsp;6

&nbsp;
</pre> </font>

<div class="p"><!----></div>
treats them as rows. Now we have two matrices that can be combined. 

<div class="p"><!----></div>
<b>Exercise 18</b> Verify that <tt>rbind(C,D)</tt> works,  <tt>cbind(C,C)</tt>
works, but <tt>cbind(C,D)</tt> doesn't. Why not? 

<div class="p"><!----></div>
     <h3><a name="tth_sEc7.3">
7.3</a>&nbsp;&nbsp;Matrix indexing</h3>
Matrix indexing is like vector indexing except that you have 
to specify both the row and column, or range of rows and columns. For 
example <tt>z=A[2,3]</tt> sets <tt>z</tt> equal to 6, which is the (2 row, 
3 column) entry of the matrix <b>A</b> that you recently
created, and 

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;A[2,&nbsp;2:3]
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;5&nbsp;6

&nbsp;
</pre> </font>
  <font color="#FF0000">
<pre>
&#62;&nbsp;B&nbsp;=&nbsp;A[2:3,&nbsp;1:2]
&#62;&nbsp;B
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,1]&nbsp;[,2]
[1,]&nbsp;&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;5
[2,]&nbsp;&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp;8

&nbsp;
</pre> </font>

<div class="p"><!----></div>
There is an easy shortcut to extract entire rows or columns: leave out the limits,
leaving a blank before or after the comma.

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;first.row&nbsp;=&nbsp;A[1,&nbsp;]
&#62;&nbsp;first.row
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;1&nbsp;2&nbsp;3

&nbsp;
</pre> </font>
  <font color="#FF0000">
<pre>
&#62;&nbsp;second.column&nbsp;=&nbsp;A[,&nbsp;2]
&#62;&nbsp;second.column
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
[1]&nbsp;2&nbsp;5&nbsp;8

&nbsp;
</pre> </font>

<div class="p"><!----></div>
(What does <tt>A[,]</tt> do?)

<div class="p"><!----></div>
As with vectors, indexing also works in reverse for assigning values to matrix 
entries. For example,

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;A[1,&nbsp;1]&nbsp;=&nbsp;12
&#62;&nbsp;A
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,1]&nbsp;[,2]&nbsp;[,3]
[1,]&nbsp;&nbsp;&nbsp;12&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;3
[2,]&nbsp;&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;&nbsp;6
[3,]&nbsp;&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp;9

&nbsp;
</pre> </font>

<div class="p"><!----></div>
The same can be done with blocks, rows, or columns, for example

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;A[1,&nbsp;]&nbsp;=&nbsp;c(2,&nbsp;4,&nbsp;5)
&#62;&nbsp;A
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,1]&nbsp;[,2]&nbsp;[,3]
[1,]&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;5
[2,]&nbsp;&nbsp;&nbsp;&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;&nbsp;6
[3,]&nbsp;&nbsp;&nbsp;&nbsp;7&nbsp;&nbsp;&nbsp;&nbsp;8&nbsp;&nbsp;&nbsp;&nbsp;9

&nbsp;
</pre> </font>

<div class="p"><!----></div>
If you use <tt>which()</tt> on a matrix,  R&nbsp;will normally
treat the matrix as a vector - so for example
<tt>which(A==8)</tt> will give the answer 6 (figure out
why).  However, <tt>which()</tt> does have an 
option that will treat its argument
as a matrix:

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;which(A&nbsp;==&nbsp;8,&nbsp;arr.ind&nbsp;=&nbsp;TRUE)
&nbsp;
</pre> </font>
  <font color="#0000FF">
<pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;row&nbsp;col
[1,]&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;2

&nbsp;
</pre> </font>

<div class="p"><!----></div>
 <h2><a name="tth_sEc8">
8</a>&nbsp;&nbsp;Other structures: Lists and data frames</h2>

<div class="p"><!----></div>
     <h3><a name="tth_sEc8.1">
8.1</a>&nbsp;&nbsp;Lists</h3>
While vectors and matrices may seem pretty familiar,
lists are probably new to you.
Vectors and matrices have to contain elements that
are all the same type:
lists in  R&nbsp;can contain anything - vectors, matrices,
other lists ...
Indexing is a little different too, use <tt>[[ ]]</tt> (rather
to extract an element of a list by number or name, or
<tt>$</tt> to extract an element by name (only).
Given a list like this:

<div class="p"><!----></div>
  <font color="#FF0000">
<pre>
&#62;&nbsp;L&nbsp;=&nbsp;list(A&nbsp;=&nbsp;x,&nbsp;B&nbsp;=&nbsp;y,&nbsp;C&nbsp;=&nbsp;c("a",&nbsp;"b",&nbsp;"c"))
&nbsp;
</pre> </font>

<div class="p"><!----></div>
Then <tt>L$A</tt>, <tt>L[["a"]]</tt>, and 
<tt>L[[1]]</tt> will all grab the first element
of the list.

<div class="p"><!----></div>
     <h3><a name="tth_sEc8.2">
8.2</a>&nbsp;&nbsp;Data frames</h3>
Data frames are the solution to the problem that vectors
and matrices (which might seem to be the most natural
way to store data) can only contain a single type of
data, but most data sets have several different kinds of
variables for each observation.  Data frames are a
hybrid of lists and vectors; internally, they are a list
of vectors that can be of different types but all have
to be the same length, but you can do most of the same
things with them (e.g., extracting a subset of rows)
that you can do with matrices.  You can index them
either the way you would index a list, using
<tt>[[&nbsp;]]</tt> or <tt>$</tt> - where each variable
is a different item in the list - or the way you
would index a matrix.  Use <tt>as.matrix()</tt>
if you have a data frame (where all variables are the same type) that
you really want to be a matrix, e.g. if you need
to transpose it (use <tt>as.data.frame()</tt> to
go the other way).

<div class="p"><!----></div>

<h2>References</h2>

<dl compact="compact">
 <dt><a href="#CITEFussman+2000" name="Fussman+2000">[1]</a></dt><dd>
G.&nbsp;Fussmann, S.&nbsp;P. Ellner, K.&nbsp;W. Shertzer, , and Jr. N.&nbsp;G.&nbsp;Hairston.
 Crossing the Hopf bifurcation in a live predator-prey system.
 <em>Science</em>, 290:1358-1360, 2000.

<div class="p"><!----></div>
</dd>
 <dt><a href="#CITEIhakaGentleman1996" name="IhakaGentleman1996">[2]</a></dt><dd>
Ross Ihaka and Robert Gentleman.
 R: A language for data analysis and graphics.
 <em>Journal of Computational and Graphical Statistics</em>,
  5(3):299-314, 1996.

<div class="p"><!----></div>
</dd>
 <dt><a href="#CITEMASS" name="MASS">[3]</a></dt><dd>
Venables and Ripley.
 <em>Modern Applied Statistics with S</em>.
 Springer, New York, 3d edition, ????</dd>
</dl>


<div class="p"><!----></div>
<hr /><h3>Footnotes:</h3>

<div class="p"><!----></div>
<a name="tthFtNtAAB"></a><a href="#tthFrefAAB">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msup><mrow></mrow><mrow><mn>1</mn></mrow>
</msup>
</mrow></math></a>Ecology and Evolutionary Biology, Cornell
<div class="p"><!----></div>
<a name="tthFtNtAAC"></a><a href="#tthFrefAAC">
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msup><mrow></mrow><mrow><mn>2</mn></mrow>
</msup>
</mrow></math></a>Zoology, University of Florida
<br /><br /><hr /><small>File translated from
T<sub><font size="-1">E</font></sub>X
by <a href="http://hutchinson.belmont.ma.us/tth/">
T<sub><font size="-1">T</font></sub>M</a>,
version 3.70.<br />On 30 Aug 2005, 14:48.</small>
</body></html>
