In its simplest form, R can be used as a calculator.

`## [1] 2`

`## [1] 2.693147`

`## [1] 1`

Printed results are interesting, but not very useful if we can not reuse them.

In R, two operators (

=,<-) can be used to assign a value to a variable. However, by convention<-is preferred. Use keysalt + -to automatically insert the<-symbol.

You have to be careful when choosing a variable name. There are few rules to respect.

Can contain both letters and numbers, but must start with a letter (good: x123, bad: 1x23)

Should not contain special characters (à, è, æ, ø, å, ?, %, ’, $, etc.)

Should not be the same as a R function or predefined variable name (

`cos`

,`sin`

,`pi`

,`min`

, etc.)

- numeric:
`12.3`

,`pi`

,`1`

, etc. - character:
`"a"`

,`"text"`

,`"long text with space"`

- boolean/logical:
`TRUE/1`

,`FALSE/0`

- date:
`2016-01-31`

(use ISO format, large -> small)

We can use the `class()`

function to get the type of a variable.

`## [1] "numeric"`

What is the class of `x`

?

- Vectors are arrays of successive elements. Elements of a vector can be, for example:
**numeric**,**character**,**logical**,**date**. - Vectors are created using the
`c()`

function with`,`

used to separate elements.

```
x <- c(1, 4, -1, 10, 5.4) # Vector of numeric values
y <- c("This", "is", "a", "vector") # Vector of character values
z <- c(FALSE, TRUE, FALSE, FALSE, TRUE) # Vector of logical values
class(z)
```

`## [1] "logical"`

What is the class of this vector?

- Particular elements of a vector can be accessed using the
**operator.** - The first element of a vector is located at position 1.

`## [1] 1`

`## [1] 11`

Given the vector `x`

:

What is the value of:

Given the vector `x`

:

What is the value of:

We can also apply a function to all elements of a vector.

`## [1] 0.5 2.0 -0.5 5.0 2.7`

`## [1] 0.8414710 -0.7568025 -0.8414710 -0.5440211 -0.7727645`

Vectors and matrices of the same size can be operated on arithmetically, with the operations acting element-wise.

`## [1] 4 4 9 2`

What will happen when executing the following code:

What will happen when executing the following code:

- A matrix is a 2-dimensional entity containing a number of rows and columns.
- The upper left element is located at line 1 and column 1.

\[ A_{m,n} = \begin{bmatrix} a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m,1} & a_{m,2} & \cdots & a_{m,n} \end{bmatrix} \]

To understand how matrix work in R, lets create the following matrix `A`

.

```
## [,1] [,2] [,3] [,4] [,5]
## [1,] 3 13 11 16 6
## [2,] 15 1 8 25 18
## [3,] 24 5 4 10 21
## [4,] 14 12 17 2 22
## [5,] 19 9 20 7 23
```

Elements of a matrix can be accessed using the following scheme:

For example, the value at position \(A_{3,1}\) is:

`## [1] 24`

If the line or row number is omitted, the corresponding vector at line or row will be returned.

`## [1] 3 13 11 16 6`

`## [1] 16 25 10 2 7`

What is the value of:

What is the value of:

Missing values in R are represented by `NA`

(not available). All operations performed on NA values will return the value `NA`

.

`## [1] 2 3 4 NA 6`

NA can be easily identified with the `is.na()`

function.

`## [1] FALSE FALSE FALSE TRUE FALSE`

Most R functions will return `NA`

if such values are present in the data.

`## [1] NA`

You have to **explicitly** tell R what to do with `NA`

. This is often done using the `na.rm`

argument.

`## [1] 2.75`

Logical operators are another important concept. When using these operators in R, `TRUE`

or `FALSE`

will be **always** returned.

Operator |
Description |
---|---|

< | less than |

> | greater than |

<= | lesser than or equal to |

>= | greater than or equal to |

== | equal to |

!= | not equal to |

\(|\) | entry wise or |

& | entry wise and |

! | not |

`## [1] FALSE`

`## [1] TRUE TRUE TRUE FALSE FALSE FALSE`

`## [1] TRUE TRUE TRUE FALSE TRUE TRUE`

It is important to know that: `0 == FALSE`

and `1 == TRUE`

.

`## [1] TRUE`

`## [1] TRUE`

Logical operators can also be used on characters or strings.

`## [1] FALSE`

`## [1] FALSE`

`## [1] TRUE`

Logical operators can also be combined.

A |
B |
AND (A & B) |
OR (A | B) |
NOT (!A) |
---|---|---|---|---|

FALSE | FALSE | FALSE | FALSE | TRUE |

FALSE | TRUE | FALSE | TRUE | TRUE |

TRUE | FALSE | FALSE | TRUE | FALSE |

TRUE | TRUE | TRUE | TRUE | FALSE |

Logical operators can also be combined.

`## [1] FALSE`

`## [1] TRUE`

What will be the result of the following code?

A function is a procedure that performs different commands and returns only the computed result.

The user does not see the source code of the function since he/she is (usually) only interested by the result.

For example, whitin the following code, the `mean()`

and `rnorm()`

functions are used to calculate the average of 10 numbers generated randomly.

`## [1] 0.1565971`

We know what it does, but we do not know how it has been programmed internally.

The order of the parameters passed to a function is **important**. There are two ways for passing parameters to a function.

```
my_function <- function(param1, param2, ...) {
# Do something super cool with param1 and param2
# Return the result(s) of what you did in the function
return(...)
}
```

The function `return()`

is used to return the final result(s) of a function.

Lets create a function that will sum 3 values.

```
my_first_r_function <- function(x, y, z)
{
# Do something here with arguments x, y, z
result <- x + y + z
# Return the value of result
return(result)
}
```

`## [1] 6`

```
my_first_r_function <- function(x, y, z)
{
# Do something here with arguments x, y, z
result <- x + y + z
# Return the value of result
return(result)
}
```

`## [1] 29.7677`

To rapidly create a new function, start typing “fun” and press tab and select snippet.

Sometimes, a function needs a minimum number of parameters to be invoked. These are called

**required parameters**.On other hand,

**optional parameters**have default values and can be omitted.

In some cases, parameters of a function must be **obligatory** provided by the user.

`## [1] 6`

`## Error in my_first_r_function(1, 2): argument "z" is missing, with no default`

Optional parameters mean that the function has been written with default values for some of its parameters. If the user do not specify them, the default value will be used.

`## [1] 5`

`## [1] 4`

Control flow describes how functions should be evaluated according to condition(s) when your code is being executed.

For example, you could tell R to execute one particular task if the value of a variable is greater than a specific threshold.

An **if condition** offers the possibility to execute a code section if a certain condition is `TRUE`

.

It is also possible to add an `else`

clause to specify what to do if the condition is not met.

`## [1] "A"`

For simple conditions, it can be convenient to use the `ifelse()`

function.

```
# Test if pi is < than 3. If yes, return "A", if no return "B"
ifelse(test = pi < 3, yes = "A", no = "B")
```

`## [1] "B"`

To rapidly create an

if condition, start typing “if” and press tab and select snippet.

What will be the value of `b`

after this code is executed?

Loops allow repeating operation(s) a number of time. There is two main types of loop in R: (**1**) *for loop* and (**2**) *while loop*. In both cases, everything inside the curly brackets **{}** will be repeated a certain number of time.

For loops are usually used when **we know in advance** how many loops we need to do. This type of loop depends on a counter that determines when the looping should stop. For loops are defined as follows:

At each iteration, the variable counter will take a new value based on all elements contained in vector.

For example, the following **for loop** will be executed five times.

```
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
```

```
## [1] "a"
## [1] "c"
## [1] "b"
```

To rapidly create a

for loop, start typing “for” and press tab and select snippet.

First, use the following code to create a numeric vector with 100 random numbers.

Create a loop that will calculate the average of all elements in the vector `vec`

. The answer should be: -0.1567617.

While loops are usually used when **we do not know in advance** how many loops we need to do. A while loop will execute a block of commands until the condition is no longer satisfied.

In this example, the loop is executed as long as \(x \geq 2\).

```
x <- 10
while (x >= 2) {
print(x) # Print the value of x
x <- x - 2 # Decrease the value of x by 2
}
```

```
## [1] 10
## [1] 8
## [1] 6
## [1] 4
## [1] 2
```

To rapidly create a

while loop, start typing “while” and press tab and select snippet.

Create a **while loop** that will divide a given number (ex: \(x = 12344\)) by 3 until the result is not smaller than 0.001.

How many loop were done?

There are already many functions defined in R.

R has a rapid growing community -> users developing external functions which are regrouped into **packages** or **libraries**.

The number of downloaded packages everyday is impressive.