# Introduction to R - Part 1

## 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.

## Working with R

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/')``

### Creating a variable

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

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

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``````

### 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``

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:

`` 8``

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

``temp <- 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).

## 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.