In these getting started notes, I'm assuming you are a business user in the US. Thus, I'll be directing you to the US CRAN mirror for downloads and I'm assuming you are using windows. If you are based elsewhere, please use your local CRAN mirror. R also runs under UNIX, Linux and MacOS. See your CRAN homepage for these other distributions.
Downloading R
First, download the current version of R for Windows (rw?????.exe) to a place you can find it. Your desktop works just fine. You may want to browse the Read Me whilst the setup file is downloading. Once downloaded, simply run the set-up file by double-clicking on it. A typical Windows setup dialog will start. Accept the default settings, except do check the option to create a short-cut on your desktop. I have never had any problems with the install, so hopefully all has gone smoothly. If it has, you may want to delete the setup download.
Trying R
Let's now give R a simple test. Start R by double clicking on the shortcut created by the setup process. R will now start. You should see an empty "RGui" window. After a moment the welcome text will appear followed by the R command prompt: ">".
OK, let's try your first R command! Type
1+1
followed by an Enter.
Now for something more exciting. Type the two lines:
x <- rnorm(1000)
hist(x)
The first line creates a 1000 element vector, x, filled with normally distributed random numbers. Then you asked R to plot the histogram of x.
Enough excitement!
Help with R
Now I want you to look the extensive help system that comes with R. R help is available in four forms:
1) Well formatted PDF files that you can read on-line or printed out. One of the great things about the R project is the completeness of the R documentation. (If you don't have an Acrobat reader on your PC, now would be a good time to install one from the Adobe site).
From the Windows menu bar look at: Help|Manuals|An Introduction to R
This 100 page manual is one place to start learning R. Read, at least, Chapter 1 and work through the sample session in Appendix A. Chapters 2 through 12 give a rather complete, if somewhat terse, overview of R.
2) The same content is also in HTML (browser) form: Help|Html Help
with some additional links.
3) FAQ's from Help or on CRAN (which will typically be more up to date than your local copy)
4) Interactive help by typing ?function. Try typing "?hist" at the ">" prompt.
There are actually a few more ways to get help, but this is enough for now.
Quiting R
Quitting R is easy. Just type "q()" or, in Windows, select File|Exit or click the top left "X" close icon. Yes, go ahead and quit. R will ask "Save workspace image?" Reply "No" for now. Often you would want to save your work!
Setting up your Workspace
R keeps its files in its working folder. By default, R uses the install folder in "Program Files." It's a good idea to keep your various projects separated by setting up different folders for each. My personal convention is to have a set of folders like: C:\Data\LearnR\R, where "LearnR" could be your first project.
In Windows, it's easy to set up a short-cut on your desktop for each project by
1) Create your working folder. Mine would be: "C:\Data\LearnR\R"
2) Copy the default R short cut on your desktop and paste a copy of it back on the desktop.
3) Rename the copy, for example, to "Learn R"
4) Right click on your new shortcut and open the Properties dialog.
5) The "Start in" entry will be something like: "C:\Program Files\R\rw1090". Change it to your working folder. Mine is: "C:\Data\LearnR\R"
Test your new short cut!
R should open as before. Open the Change Directory dialog with the menu bar: File|Change dir... to verify that R is starting where you intended. If it is not, quit R and check your short cut properties.
Type "ls()" to list the objects in your workspace. R should reply "character(0)", which is an empty character vector. Now type "x <- rnorm(1000)" as above. Now when you type "ls()", R should know about your newly defined vector x.
Quit R and take a look in your working folder. You should see two files .RData and .Rhistory. The next time you start R as above, R will load these so you can start again just where you left off!
Next Steps
Try doing some R. Try Appendix A in the Introduction to R. CRAN's Contributed Documentation page has a number of short getting started guides which I found helpful in the beginning.
I would also recommend starting with one, or both, of the following books:
Peter Dalgaard's Introductory Statistics with R which was my first R book. Peter starts with a 40 page introduction to R which I found more novice friendly than the R docs. This 250 page book covers a lot of ground going beyond the typical introductory stats text. Very practical with a lot of R examples.
John Maindonald and John Braun's Data Analysis and Graphics Using R - An Example-based Approach The title, almost, says it all. Somewhat longer (350 pages) with more graphics emphasis but less on R as a language. Also has a lot of R examples - interestingly in footnotes so the code does not "disrupt" the flow of the text. Solid data analysis, including multi-level models, tree-based classification/regression and multivariate exploration & discrimination.
(Remember books purchased through this blog benefit the R Foundation.)
Have Fun!
Recent Comments