| Math 2200 - Using R |
| || || || |
Steps to use R on your own computer:
- 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.)
- 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 ">".
- 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!)
- Get help on R commands. For example, try entering "? boxplot" to see all the options for boxplot.
A sample R session:
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.
- 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:
Reads the Cities Cost of Living data from text file into the cities data table (or "frame").
Displays the contents of cities.
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.
Displays a histogram of the Cost.of.Living variable.
Displays a boxplot of the Cost.of.Living variable.
Displays the InsectSprays table of data, which is built into R as a sample data set.
- 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".
See also the Introduction on the Comprehensive R Archive Network.