Math 2200 - Using R
 

 

Home
Syllabus
Schedule
Links
 

Using R
 

MyStatLab
 

 

 

 

 

 

 

 

 

 

 

 

Using R

Steps to use R on your own computer:

  1. Download R, and install it. There's an install package for Mac OS X, and presumably for Windows. There are Linux packages for Redhat, Suse, Ubuntu, and probably others (or you can build it yourself.)
  2. Run R. On the Macintosh, there will be an R.app or R64.app in your Applications dictory -- start either. On Windows, it will show up under your Programs menu. On Linux, the binary name is R.
    You will get a command-line prompt ">".
  3. Get yourself some data. You can read the TEXT FILE (.TXT) data from the CD that comes with your book with the read.delim("filename.txt") command.
    If you don't give the full pathname, R will try to read it from its current working directory. On the Mac, I've found it convenient to use the Misc | Change Working Directory command to go to the right directory. (You might also find it convenient to copy the files to a directory which is shorter to enter!)
  4. Get help on R commands. For example, try entering "? boxplot" to see all the options for boxplot.

A sample R session:

  1. Start R, and change the working directory so that you have the file Ch04_Cities.txt from the CD with your book in it. Then type the following commands:
  2. cities<-read.delim("Ch04_Cities.txt")
         Reads the Cities Cost of Living data from text file into the cities data table (or "frame").
  3. cities
         Displays the contents of cities.
  4. plot(sort(cities$Cost.of.Living))
         Plot the cities cost of living, in order. (This isn't too interesting for this example.) cities$Cost.of.Living tells R to look at the cities table, and the Cost.of.Living variable inside this table.
         Note that R is case sensitive -- cities$cost.of.living won't work!
         You may need to go to another window to see the result of your plot.
  5. hist(cities$Cost.of.Living)
         Displays a histogram of the Cost.of.Living variable.
  6. boxplot(cities$Cost.of.Living)
         Displays a boxplot of the Cost.of.Living variable.
  7. InsectSprays
         Displays the InsectSprays table of data, which is built into R as a sample data set.
  8. boxplot(InsectSprays$count ~ InsectSprays$spray)
         Display side-by-side boxplots of the count variable for each spray, as discussed in class. Here the tilde ~ means "plot the first variable against the second variable".
I will often show in class how to do things with R, and I will post the commands I used to the course home page. I encourage you to take a "cookbook" approach to R -- I've given you the recipe for histograms and simple boxplots above, for example.

See also the Introduction on the Comprehensive R Archive Network.