In this class we will be using R to do our statistics. R is a language and environment for statistical computing and graphics. This tutorial won’t cover all of R–that’s several books worth of material. Instead, the goal is to give you enough information to get you started (and make you dangerous!)
Double-click the R icon on your desktop. When you do so, you will see the R console appear. This is where you will type your commands to tell R what to do. The greater-than sign (>) is the R prompt, which tells you that R is ready for your input.
The best way to learn R is to try things and find out what they do. Try typing this, and then press ENTER.
3 + 4
Here’s what you will see on the screen. The [1] means
that R is showing you the first number it computed.
> 3 + 4 [1] 7
You have just found out that R works like a calculator.
(The [1] means that the output shows item one
of the results.) Now try typing this:
3 + 4 # addition
As you see, you get the same results.
The # addition is called a comment.
Any time you type a #, R will ignore it and everything
following it on that line. We use comments to give information to
other humans that are reading our commands. In many of the following
examples, I will be using comments. You do not have to type
them in. Remember, the comments are for your information;
R doesn’t care about them. Try typing these calculations
and see what you get.
3 * 4 # multiply 3 / 4 # division 3 ^ 4 # raising to a power 3 + 4 * 5 # notice order of operations sqrt(4) # square root abs(3 - 5) # absolute value
In all these examples, I left a blank before and after the operator. Do you need to do this? Would typing the calculation without blanks work? Try it and find out!
3*4
From here on, the examples will show you what happens when you type them in R. Things you should type will appear in red.
What if you have a number or expression that you want to use over and over again in a calculation, but don’t want to have to type it every time? You can store your number in an object. Think of an object as a “mailbox” with a name on it, and inside that mailbox is a number that you want to use.
> x <- (3 + 7 + 9 + 14) / 4
>
The arrow <- (typed as a less than sign followed
by a minus sign) tells R to take the result of the calculation (on
the right side) and assign that value to an object named x.
This assignment doesn’t produce any output. If you want
to see the result, you can just type:
> x
[1] 8.25...and there it is. Now you can use that result over and over again by referring to the name of the object where it’s stored:
> sqrt(x) [1] 2.872281 > x - 2 [1] 6.25 > x ^ 2 [1] 68.0625
Here are some more examples:
> height <- 65.5 > weight <- 132 > bmi <- weight / (height^2) * 703 # calculate body mass index > bmi [1] 21.62951
You can interpret these by reading the left side first or the right side first; take your pick:
| Left to Right | Right to Left |
|---|---|
|
|
Now you figure out the following in R. Use parentheses where you see them in the text.
age with the value 22.harold with the value 1.066numbermultiple and set it to
9 times number.multiple times 12345679. (There is no digit
8 in that number!) This is my all-time favorite math trick.
Type the following, and see if you can figure out what these operators do and how they work. See the answers
That is enough for one session. To leave R, type the following. You must type the parentheses; if you don’t, it won’t work.
> q()You will get a dialog box asking if you want to save your workspace image. If you click Yes, all the objects you have defined during this session will be saved on the hard disk. For now, click No; in the next tutorial, you will see how to save the workspace to any folder you want.