R is a free, open-source language of statistical analyses. We will use it during the labs, and you will probably want to use it for your original research project. There are 3 ways that you may use R this semester;

  1. RStudio is installed on the computers in the Summerfield lab. Using this software is the recommended way of running the labs.
  2. You may want to install it on your own laptop or desktop computer at some point during the semester; see https://www.r-project.org/ for details on how to do that. RStudio installation information can be found at https://www.rstudio.com/

Non-interactive execution of R scripts

You probably won’t do this during our course, but this is the easiest workflow to understand. When using R this way you:

  1. Write the R code (which you can think of as “the commands in the R language”) in a text editor, and save that code to your computer’s filesystem.
  2. Then you run the R code. This means you tells R’s interpreter to read the file that has your R code and execute every instruction.

Typically your code would include instructions to print the end result of calculations or write them to an output file.

Interactively using the R interpreter - Console/Output panel

You can also type R statements at an R prompt, which looks like a greater than sign:

>  

You will see this in the panel labelled “Console” in RStudio.

Any R command typed or pasted in at the prompt will be executed by the R interpreter as soon as it detects a Return (Enter) key press.

Using a code panel to write longer sets of commands

This mode of using R lies between one-line-at-a-time execution and non-interactive execution.

In RStudio:

Note that in RStudio, the output of the code execution shows up in the Console/Output panel. RStudio shows line numbers to the left of your code.

Code written vs Code executed

Note that there is a crucial difference between command that you can see on the screen (because you typed or pasted them in) vs code that has been executed.

When you execute code you are usually telling the R to:

If you have a right triangle with side lengths of 3 and 4 you can find the length of the hypotenuse using the Pythagorean theorem that states that +  =  where c is the length of the hypotenuse. You can tell R to calculate this hypotenuse tersely:

sqrt(3^2 + 4^2)

Or with variables:

a = 3

b = 4

hyp.sq = a^2 + b^2

c = sqrt(hyp.sq)

Note that we define a variable with = sign. The ^2 operation is how we tell R to square a number.

Both code block perform the same calculations, but if you execute them you will see different effects.

The first will show the answer (5) in the Console/Output panel.

The second will show no output (in RStudio, the Console window will show the lines of code that was executed, there is no output from the code). This is because the “return value” of the sqrt function has been captured by assigning it to a variable. If you want to see the value now held in the variable c you would have to execute:

print(c) 

If R computes something (or fetches a variable from memory), but the result is not captured in a variable, then the default behavior is to print the value to the console. That is why you saw a 5 printed out if you ran the first statement.

Typing the variable name at the console (or using print and passing in the variable name) lets you see what values are held in R’s memory under different variable names. In RStudio you can also browse the variables in memory using the Environment panel.

A note on output

Note that when you print out the value of a variable holding the number 5, you actually see:

[1] 5

In the output. This happens because R treats any variable as a vector of objects. So the number 5 is actually stored in the first slot of a vector of numbers.

If you have a long vector the numbers in brackets on the left side of the output tell you what index of the variable is starting each row. That can be useful information. For instance if you execute:

x = (1:20)^2

print(x)

you will have created a vector of the squares of all of the integers from 1 to 20, stored that vector of numbers in a variable called x and then printed its value.

The output will probably wrap to more than one line, so the index markers might help you interpret where in the vector each value sits.

A note on semicolons

They are usually optional in R. They are useful it you want to put 2 statements on the same line:

x = 4 y = 5

is an error, but:

x = 4 ; y = 5

assigns values to x and y.

A note on typesetting

In the labs we will put R code in fixed width font like this and instructions in non-fixed width fonts. Note that variable in the mathematical (not programming) sense are usually in a slanted font like Y or .