Chapter 2 A Gentle Introduction to R

R is a powerful statistical programming language. However, as we’ve seen, when we start R itself, only a command-line window appears. Even when we use RStudio, point-and-click options for statistical analysis aren’t available in the menus. It’s not surprising that new users can be intimidated and confused when they first work with R.

In this section, we’ll start working with R. I’ll focus on simple but useful tasks one might typically do in R. Rather than trying to teach you all of R up front – and hoping you remember it when we get to a topic towards the end of the course – I’ll introduce R functions as they become relevant to the topics we’re studying.

2.1 R as a Calculator

R is always open on my computer desktop. In part, that’s because I use R for even the most basic calculations – literally, as a simple calculator.

Arithmetic is easy is in R and steps for doing so will likely feel natural for you. For example, we can add two numbers

> 2+2
[1] 4

We can subtract one number from another

> 7-6
[1] 1

We can multiply

> 5*6
[1] 30

And we can divide one number by another

> 56/7
[1] 8

Exponents are easily handled as well. For example, to calculate \(3^2 = 3*3\) we would type

> 3^2
[1] 9

To calculate \(4^3 = 4*4*4\)

> 4^3
[1] 64

Interactive Example

Take a second to look over the examples generated below. If you’d like to see another example, click “Show Another.” Prove to yourself that you understand the calculations and can do them yourself. In R/RStudio, enter the same or similar expressions as below and hit Return.


R is also a powerful scientific calculator. In fact, there are too many functions for us to review here. However, we’ll see some functions again and again throughout this class. For example, we’ll often want to calculate the square root of a number \(\sqrt{x}\)

> sqrt(8)
[1] 2.828

Euler’s number \(e = 2.7182818284...\) frequently appears in statistics, as does raising \(e\) to a power: \(e^x\). In R, we calculate \(e^x\) using the exp() function. Here, we’ll calculate \(e^3 = \exp(3)\)

> exp(3)
[1] 20.09

To calculate a logarithm base \(b\) of \(x\), we use the log() command

> log(8, base=2)                  # 2^? = 8
[1] 3
> log(10, base=3)                 # 3^? = 10
[1] 2.096

If we don’t provide log() with a base argument, it defaults to base \(e\) — i.e., a natural logarithm.

> log(10)                 # e^? = 10
[1] 2.303
> log(2.7182818284)                 # e^? = e
[1] 1

The fact that log() defaults to base \(e\) can be confusing. Typically, we would write \(\ln x\) for the logarithm base \(e\) of \(x\) and \(\log_{10} x\) as the logarithm base 10 of \(x\). In R, log(x) calculates the natural log of \(x\), unless we provide a different base \(b\).

Lastly, we can calculate \(x!\) in R using the factorial() function.

> factorial(5)                 # 5! = 5*4*3*2*1
[1] 120

Interactive Example

Take a look at the examples below. To generate another set of examples, click “Show Another.” Use R/RStudio to prove to yourself that you understand how to perform the calculations.


2.2 Assigning Values to Variables

One of the most important aspects of a programming language is the ability to assign values or calculated results to a variable name. We can think of the variable name as a storage bin for the value. R allows for different types of assignment under different conditions (more on this shortly). However, the most common assignment operator is the backward arrow <-, which is composed of a less than sign < and the dash -.

For example, suppose we want to assign the number 2 to the variable x:

> x <- 2

Notice that that R didn’t produce any output as part of the assignment. Instead, look at the Environment tab in the right-side pane. You should now see the variable (or object) x exists in the current working environment and it has a value of 2.

At this point, we can access the value in x simply by referring to the variable name. If we want to display the contents of x, we simply type x at the console and hit Return

> x
[1] 2

We can use x in other calculations

> x + 1
[1] 3

Here, R substitutes the value held in x (2) into the above expression and then adds 1 to it.

We can do this for multiple variables

> y <- 5
> z <- 3 + 9/3

Let’s examine the values of y and z

> y
[1] 5
> z
[1] 6

We calculate expressions that are composed of the variables themselves

> x*(y-z)
[1] -2

and we can assign those calculations to a variable name

> w <- x*(y-z)
> w
[1] -2

Interactive Example

This example demonstrates assignment to variable names and simple calculation using variable names. Click “Show Another” to generate additional examples. Using the R/RStudio console, prove to yourself that you understand how to perform the assignments and calculations by typing in similar expressions.


2.3 Working with an R Script

2.4 Vectors


Interactive Example


2.4.1 Basic Operations on Vectors


Interactive Example


2.4.2 Element-by-Element Operations on Vectors


Interactive Example


2.5 Loading Data