Unit No. 1 - 2

Introduction to R

By Kathy Chu

Ris a statistical and graphics package and is used extensively in the analysis of gene expression microarray data. R is available free of charge for Unix, Windows and Macintosh systems. R and associated software and documentation are available at the CRAN (Comprehensive R Archive Network) web site. There are several mirror sites around the world. So, you may prefer to use a mirror closer to you to download the setup program for R. You need only install the precompiled binary distribution of R for Windows, Macintosh, and Linux systems. You do not need to install the source code for R.

I assume that most of you are Windows-users. I am giving the step-by-step instructions to install R for Windows here. If you are users of other operation systems, please follow the installation instructions on the CRAN site.

Download and install the current version of R for Windows

  1. Go to R website.

  1. Click on CRAN mirror under Getting Started.Choose a mirror site near you.

  1. In the box under “Precompiled Binary Distributions”, click on Windows.

  1. Next, click on the base directory.

  1. From this directory, you need to downloadR-2.13.1-win32.exe, the setup program for R.
  1. Double-click on R-2.13.1-win32.exe to install R using the default settings. The installation program will create the directory c:Program Files\R\R-2.13.1.

Starting R

  1. Start R by double-clicking on the R icon on the desktop, or by clicking on the R icon in the start menu. The R graphical user interface (GUI) command window will open.The R's command promptindicated by a greater-than sign () is ready for you to enter commands. That is, you interact with R through a command-line interface – you type in a command and R responds.
  2. You have three ways to gethelp within R:

a.Within R, use the menu bar: Help:R language (html).

b.At the command prompt, type, for example, ?read.table or help(read.table) to get help on the function read.table.

c.At the command prompt, type, for example, help.search("read") or apropos("read") to search for things in R related to the word "read." Alternatively, go to the menu bar: Help:Apropos

  1. You can enter the commands directly in R, and then execute them by pressing the return key. Since we often make a lot of typing mistakes, I suggest you open anR text editor using the menu bar: File: New scriptto edit and save R commands. That is, you type and select the commands in R Editor, and thenpress Ctrl-R or using the menu bar: Edit: Run line and selection, orsimply click on the 3rd button (run line and selection button) on the R toolbar to copy-paste and execute the commands in R.

You can also create a script file containing a set of R commands on an R Editor using the menu: File: New script. The script can be saved in the file .R(say, fun.R), and run later by using source( “fun.R”) command.

  1. R keeps all of the variables you create as it runs. When you endan R session, you may want to save your work in an R workspace. By default, R will use the folder in which the executable program exists as its workspace. You may change the workspace for R using the menu bar: File: Chang dir …. My suggestion for you is to create a new folder where you might keep data and R sessions for this course. So, each time you start R, you can change the workspace to this diectory (i.e. folder). Notice that R save your work of a session in the file .Rdata. You can reload the saved workspace using the menu bar: File: Load Workspace. Notice that commands you type in R can also be saved in the file .Rhistoryin the same directory as the .Rdata file.
  2. You can exit R in at least three ways:
  3. Click on the X in the top-right corner of the GUI.
  4. Type q() at the command prompt.
  5. Use the menu bar: File:Exit. In each case, you will be asked, "Save workspace image?" Click "Yes" to save your work in the file .RData, "No" to discard your work, or "Cancel" to return to R.

Computing with R

The best way to learn R is by using it. You may type the commands by following the examples below and see what happens as a result.

1.You can use R as a calculator:

  1. You can use either “=” sign or “- ” sign to assign a variable. R can do calculation with arrays/vectors.

3.R has many build-in functions. To use a function, just type its name followed by the paratheses with arguments in between.

4.R includes a number of build-in datasets. To access any of these datasets, type data(dataset) where dataset is the name of the dataset you want to access. For information about the dataset, type help(dataset).

5.R can also read data from a file. For instance, a data file named height.txt containing mother’s and daughter’s heights is created using Excel and then saved in the directory/folder named math570.

In R command window, go to menu bar: File:Change dir…to change the workspace directory to math570.

Then, type in the following commands in R to read height data.

6.R has many build-in plotting functions to producehigh quality graphics.

R can place several plots side-by-side.

After making a plot, select from the menu barHistory:Recording. After making further plots, you may use the Page Up and Page Dn keys to go back and forth through the previous plots you've made.

R can save its graphics output in a file (for instance, a .pdf file) that you can include in a LaTeX document. Click on the menu bar:File:Save as ->PDF.

Documentation and references

CRAN has a list of contributed documentation. Here is a summary of tutorials and references:

  • R: A Self-Learn Tutorial,University of Bristol (UK) Statistics Group.
  • Simple R –Using R for Introductory Statistics, by John Verzani.
  • R for Beginners, by Emmanuel Paradis.
  • Kickstarting R, by Jim Lemon.
  • Data Analysis and Graphics Using R by Maindonald and Braun.
  • R reference card, by Tom Short.

Exercise

Go over all of the sections in R: A Self-Learn Tutorial. Typeset your work for the 2 exercises in section 3(include R code and output) with LaTeX ( Hint: Copy the R code and the output from R, then paste/insert this large chunks of code and output in a LaTeX file using \begin{verbatim} …… \end{verbatim} commands in LaTeX.), submit only the compiled pdf writeup. (You do not need to submit the LaTeX source code .tex file.)