Introduction to R - Part 1


Before starting this tutorial make a new folder (you can put it wherever you like and call it whatever you want, just remember where it is) and download the following file into it:
TMS_data.txt
You may need to right click and choose ‘Save link as…’ to download TMS_data.txt

Download the script for this tutorial: Intro to R & RStudio.R


Installing R and RStudio

At the University of Reading

  • Both are available on University Machines Via AppsAnywhere
  • Make sure you select the latest version of R and RStudio as some packages may not work with older versions.

On your personal machine


Working with R

Setting your working directory

The first thing you will want to do in any R session is set your working directory. The working directory is the folder on your computer that R is looking in for files and folders. If you don’t set it then R may not be able to find files that are important to your analysis.

To set your working directory first press the icon with 3 dots on the right hand side of the ‘Files’ panel:

This will allow you to select a directory on your computer. Select the folder you created at the beginning on the tutorial. Once selected you should be able to see the files you downloaded in the Files panel.

Next, click the More icon in the Files panel and select set as working directory and you’re ready to start!

You can also do this in the console or a script by using the setwd() function, like so:

setwd('path/to/folder/')

You can use the Tab key to auto-complete commands in the console.
This includes setting paths, just start entering the path, press Tab and then use the arrow keys to choose the folders and files you need.

Creating a variable

Let’s create out first variable. Enter the following code:

temp <- c (1, 3, 5, 6, 17, 8)

R uses both the <- and = symbols to assign values to variables. Generally <- is preferred, and you can use Alt + - as a quick way to insert it.

What we are doing here is telling R to create a vector of numbers and place them in the variable called temp.
You should see the variable temp appear in the Environment panel with information about it, including its type (num: numeric), length ([1:6]) and contents.
From here we can start doing things with temp. Let’s run a couple of functions on temp. Functions are pre-defined scripts that perform some action on the variable passed to them.

str(temp)                 # Show temp the structure of temp
summary(temp)             # Show some basic descriptive statisitics for temp
sum(temp)/length(temp)    # Manually calculate the mean for temp
mean(temp)                # Use the mean function on temp (should give exactly the same answer)
plot(temp)                # Plot temp
order(temp)               # Give the index of cells in order of magnitude

If you don’t feel like typing out all the commands from this site you can copy them by clicking the clipboard symbol in the top right of the code block.

Indexing in R

Take a look at the plot and the output from order(). It looks like the 6th value is smaller than the 5th one, let’s change it so that each number in this vector is larger than the last.
Rather than recreate the entire variable we’ll use indexing to select a single number from temp.
Lets start by looking at what number is in the 6th position in temp:

temp[6]

As you can see we do this by indicating which variable we want to look at, and then indicating the position by placing it in square brackets. The result of this should be:

[1] 8

Now let’s assign a new value to this position, this works in much the same way as creating a new variable.

temp[6] <- 20

Nothing immediately obvious has changed, but if you look in your Environment the 6th number in temp is now 20, and if you plot temp again (by running plot(temp)) then you should also see the change.

Importing Data

R can read data in from files, it prefers plain text files like .txt or .csv but can also handle complex files like SPSS’s .sav files.
You can either import files in manually by clicking the Import Dataset button at the top of the environment panel or you can enter a command in the console:

dat1 <- read.delim('TMS_data.txt')

You should see dat1 appear in your Environment, with 9600 obs. (rows) and 11 variables (columns).

R has a number of different functions for importing plaintext data depending on its structure:
read.delim() reads tab separated value files.
read.csv() reads comma separated value files.
read.csv2() reads colon separated value files.
read.table() reads most other types of plain text files, but you will need to tell it how the file is structured.

If you’re having trouble loading the data first thing you should check is whether your working directory is correct by running getwd().
If your working directory is not where the file is then take another look at Setting your working directory.


Getting help with R

Built-in help

R has built help for functions that describe what they do, show the inputs they expect, and provide some examples of how to use them.
This can be accessed using ?function name or help(function name). For example, to view the documentation for plot() you can use:

# Either
?plot

# Or
help(plot)

You can also use help.search() to search the help documentaion for key words:

help.search('histogram')

Other help

In many cases the built-in help is overly technical,and can be daunting for beginners (and even intermediate users).
But there is a large community of R users online of varying abilities, so Googling your problem or question is a good start as chances are someone else has been where you are (be sure to include the ‘R’ in the search terms so you get answers specific to R).
More specifc sites and resources are listed in the Useful resources section and more will be added as we find them.