# Chapter 2 Reminder on R

This reminder was adapted from B. Michel’s introduction to R.

R is a calculator

`## [1] 2`

To create an object in R, the syntax is `Name.of.the.object.to.create <- instructions`

:

`## [1] 1`

The online R help is very complete. You can reach it with the function `help()`

(also `?`

). For example, type `help(sum)`

(also `?sum`

) in the console to get help about the function `sum()`

.

A vector is a sequence of data points of the same type. A vector can be created with the function `c()`

. Try the following commands:

```
A <- c(1,2,10)
B <- seq(from=0,to=10,length=2)
C <- seq(from=0,to=1,by=0.1)
D <- 1:10
A;B;C;D
C[1:3]
C[-3]
3 * B
D + 2:11
```

Matrices can be defined using the function `matrix()`

:

```
M <- matrix(1:6, nrow = 2, ncol = 3)
M
dim(M)
length(M)
ncol(M)
nrow(M)
rownames(M)
colnames(M)
M[2,c(1,3)]
M[c(TRUE,FALSE),]
M %*% A
t(M)
```

A data.frame is key quantity for statistics in R. A data.frame is a matrix with each line corresponding to an individual and each column corresponding to a variable measured on the individuals. Each column thus represents a single variable (same type across all individuals).

The function`read.table()`

read the data of a (text or csv) file and import them into R as a data.frame:

```
MyData <- read.table(file= "(complete) file path",
header = TRUE,
sep = "\t",
row.names = )
```

The file argument is a character string, it can be the name of the file if the file is in the work director.

S4 classes can fit all these data types (vectors, data.frames, lists) in their attributes, which are themselves accessible via an `@`

symbol (use `object@data.frame$var1`

.

The ggplot2 package offers a powerful graphics language for creating elegant and complex plots. It is based on a so-called “Grammar of Graphics” which consists in independently specifying plot building blocks and in combining them to create just about any kind of graphical display you want. Building blocks of a graph include: data, aesthetic mapping (something you can see on a graph), geometric object, statistical transformations, scales…

For this tutorial we only give a few illustrations of ggplot2 graphics. See for instance this cheatsheet for more details.