# Basic R

## Contents |

# R reference card

A copy of this will also be handed out during the course:

# Some basic R commands

## Using R as a calculator

Start an R session by typing 'R' in a terminal window on the R server. After R has started, anything you type in the terminal will be evaluated by R after you press return.

For example:

1 + 2

Followed by return, will results in this output:

[1] 3

Or:

1 - 2 [1] -1

Or multiplication:

4 * 5 [1] 20

Or division:

20 / 5 [1] 4

## Comments in R

Any code that is preceded by **#** is not evaluated by R. So it is useful for giving comments in lines of code:

# This piece of code calculates the sum of 12 and 57 12 + 57

will result in:

[1] 69

So the line "*# This piece of code calculates the sum of 12 and 57*" is completely ignored.

## Variables in R

You can also store the result in a so-called variable, by using '<-' or '='. Say we want to store the result in a variable called 'gene':

gene <- 1 + 2

Notice that you don't see the result now. We can still get it though by typing:

gene

Resulting in:

[1] 3

## Vectors

Sometimes it is required to not just store one value in a variable, but to store multiple values. This can be done easily in R by creating a *vector*.

Example:

vectorExample <- c(10, 20, 30, 40, 50)

If we type

vectorExample

We get all the values:

[1] 10 20 30 40 50

If we just want to know one value, for instance the 4th value, we type the element we want in square brackets:

vectorExample[4]

we get:

[1] 40

## Data frames

Vectors can be combined into data frames. Say we have two vectors:

names <- c("Michiel", "Lars", "Stan") scores <- c(10, 20, 30)

we can create a dataframe containing these vectors:

namesAndScores <- data.frame(names, scores)

The result is basically a matrix of values, in this case with 2 columns named "names" and "scores". To get the first element of the second row, we type:

namesAndScores[2, 1]

We can get a complete row with:

namesAndScores[1, ]

We can get a complete column in all of the following ways (each method is identical):

namesAndScores[ , 2] namesAndScores[ , "scores"] namesAndScores$scores

## Lists

A type of variable (object would be a better name) you will encounter often is a *list*. A *list* object is literally a list of other variables and objects.

An example is for instance:

namesAndScores <- list(michiel = 1, lars = 2, stan = 3)

This will create a *list* named **namesAndScores**. If you type:

namesAndScores

You will see:

$michiel [1] 1 $lars [1] 2 $stan [1] 3

The nice thing about a *list* is that it can be used as a sort of look-up table. If we want to know the score of **Lars**, we simply type his name in the object:

namesAndScores["lars"]

and get:

$lars [1] 2

We will be using lists in this way a lot.

## Functions

### Built-in functions

A function in R is just like a standard mathematical function: you supply an input value and get back an output value.

There are built-in functions in R, like **sum(..)**. This function simply literally sums the values you supply to the function.

So if we create a vector of numbers first:

numbers <- c(1, 2, 3, 4, 5)

We can calculate the sum of the values:

sum(numbers)

we get:

[1] 15

Sometimes you can supply more than one input value to a function. The function **plot(..)** will plot values in a chart. We can supply a range of *x* coordinates with corresponding *y* coordinates. In this example we will first make a vector with 7 *x* coordinates and 7 *y* coordinates which are the square of the *x* coordinates:

x <- c(1, 2, 3, 4, 5, 6, 7) y <- x * x

which can be plotted like this:

plot(x, y)

### User created functions

You can also create your own functions. We could for instance create a function that calculates the squared value of any number or vector of numbers you supply to it:

squareNumbers <- function(x) { return(x^2) }

In these lines of code, you can see the word *function*, which indicates that the created object *squareNumbers* is a function. There is on input value specified in this function, namely *x*. Inside the function, the values found in *x* are squared with *^2* and returned to the user with **return(..)**.

To test, we first make a vector:

x <- c(1, 2, 3, 4, 5, 6, 7)

If we then use the function:

squareNumbers(x)

results will look like this:

[1] 1 4 9 16 25 36 49