PSYCH 018 Index > Introduction to R (Part 1)

Introduction to R (Part 1)

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!)

Starting R

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.

Objects

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 RightRight to Left
  • height becomes 65.5
  • weight becomes 132
  • bmi becomes weight divided by height squared, times 703.
  • Put 65.5 into height
  • Put 132 into weight
  • Calculate weight divided by height squared, times 703, and put that into bmi.

You Do It!

Now you figure out the following in R. Use parentheses where you see them in the text.

  1. 3 plus 7 minus 9 plus 14
  2. 14 times 78 plus 2
  3. 14 times (78 plus 2)
  4. the square root of (12 to the third power)
  5. (the square root of 12) to the third power
  6. Create an object named age with the value 22.
  7. Create an object named harold with the value 1.066
  8. Choose your favorite number between 1 and 9, and store that number in an object named number
  9. Create an object named multiple and set it to 9 times number.
  10. Multiply multiple times 12345679. (There is no digit 8 in that number!) This is my all-time favorite math trick.

Discovery Exercises

Type the following, and see if you can figure out what these operators do and how they work. See the answers

Leaving R

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.