Data Analysis with R, Lesson 1

Topics covered:

If you are running Linux, it is easiest just to download using a Linux tool. For example, for Ubuntu, do

sudo apt-get install r-base

For the other platforms, go to the R home page, click on "CRAN", and choose a nearby site. (It doesn't really have to be near, just up.) Then click on the "Download" link for your operating system. You may be asked to run the downloaded package for the final install step; do so.

You may wish to watch the first minute or two of this YouTube video, which walks you through the above steps.

Your screen should now have an R icon, in the Windows case.

You can now run R! Click on the icon (Windows), or simply type the letter 'R' into a terminal window (Mac or Linux).

In its basic form, R is text-oriented in terms of issuing commands, though one of its best features is excellent graphics capabilities. If you prefer a mouse-oriented interface, RStudio is really excllent, but it's easier if you stick to text for now.

When you run R, there will be a welcome message, something like

R version 3.0.1 (2013-05-16) -- "Good Sport"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: i686-pc-linux-gnu (32-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> 

Look at that last line. It shows the R prompt, the > sign, ready for your commands! Let's start with the embarrassingly simple computation, 1 + 1 = 2:

> 1+1
[1] 2
>

Here I typed "1+1", then hit the Enter key.

Sure enough, R informed me that the result is 2. The "[1]" means that this is row 1 of the results, which indeed can take up many rows for some operations. R also printed out is '> prompt again, inviting me to submit a command. (I'll suppress that in my displays below, to save space.)

There is something else that happened above, though, that you'll use all the time: If you type any expression, in this case a numerical expression, at the R '>' prompt, R will print that expression for you. In the above case, I asked R to compute 1 + 1, and it not only did so but also printed the result to the screen.

I can use the arrow keys to go back to previous commands, and use the backspace key etc. to edit. In the above case, for instance, I could use the up-arrow key to go back to the "1+1" command, backspace once to erase the second 1, and put a 2 there, yielding

> 1+2
[1] 3

Here's another example, computing the area of a circle of radius 8:

> 3.14 * 8^2
[1] 200.96

Here the asterisk '*' means multiplication, and the carat '^' means exponeniation, so we are taking 8 to the power 2 here.

We can do algebraic grouping with parentheses too, e.g.

> 3 / (4*2 + 7)
[1] 0.2

To exit R, type "q()"; on Macs and Linux, you can also type control-d.

Exercises

  1. Use R to find the volume of the Earth in cubic miles, given that the diameter is about 8000 miles. (The formula for volume of a sphere of radius r is (4/3) π r3.)

Continue

And now, on to Lesson 2.