# Chapter 3 Lab 1 - 08/10/2021

In this lecture we will learn the basic commands for programming with R. We will work in RStudio.

Place your cursor (prompt symbol) in the Console (bottom-left panel) and type the following code or any other mathematical operation:

`5+(-2/0.5)`

`## [1] 1`

R can work like a calculator and you can compute whatever quantity you need. The point is that if you close RStudio, you will lose all your code. To avoid this, I suggest to use scripts (i.e. text files where you can write and run your code). To open a new one, use the RStudio menu: File - New File - RScript. Write all your code in your script(s) and use `ctrl/cmd + enter`

to run the code in the Console. Save the script by using the menu: File - Save as…. The extension of a script is `.R`

(choose the name you prefer). In the future you will be able to open your script with RStudio without losing anything.

## 3.1 Built-in functions

Before starting using a function, it is good pratice to visualise the help page (by running `?nameofthefunction`

) in order to understand how the function is defined and which are its arguments and the default settings.

To run a function (and get the results) type the name of the function followed by round parentheses, inside which you specify the arguments of the functions (the inputs).

For example to compute the logarithm of a number the function `log`

is used. Its help page can be obtained by

` ?log`

Note that the function is characterized by two arguments: `x`

(number/vector for which the log is computed) and `base`

(the base of the log function, by default it’s the natural log).

Let’s compute the log as follows:

`log(x = 5 , base = 10) #log of 5 with base 10`

`## [1] 0.69897`

`log(x = 5) #natural log `

`## [1] 1.609438`

Argument names can be omitted. In this case it is very important to be careful about the order of the arguments passed to the function (the order is given by the function definition, check the help).

`log(5, 10)`

`## [1] 0.69897`

`log(10, 5) #warning: this is log of 10 with base 5`

`## [1] 1.430677`

## 3.2 Objects and vectors

In R it is possible to create objects by using the **assignment** operator `=`

(it is also possible to use `<-`

). As argument name you can choose any name you prefer; however, it’s better to use short and meaningful names. The following code assigns the number -1.5 to the object named `x`

(have a look to the top right panel!):

`= -1.5 x `

A vector of number is created using the `c`

function (concatenate). For example the following code is used to create a vector with 4 numbers. The vector is saved in an object named `y`

:

`= c(-1.5, 99, log(15), 6.7) y `

To visualize the values of `y`

just run the object name (remember that R is case-sensitive so that `y`

is different from `Y`

):

` y`

`## [1] -1.50000 99.00000 2.70805 6.70000`

The vector length is given by

`length(y)`

`## [1] 4`

It is possible to compute operations with vector. For example, the following code

`+ 4 y `

`## [1] 2.50000 103.00000 6.70805 10.70000`

add 4 to all the elements of `y`

. Note that R executes operations with vector **element-wise** (element by element singularly). The same element-wise approach is adopted also for computing operations involving two vectors of the same length:

```
# create another object z
= c(6.89, -10, 5.5, sqrt(log(5)))
z z
```

`## [1] 6.890000 -10.000000 5.500000 1.268636`

```
# operations with 2 vectors
- z y
```

`## [1] -8.390000 109.000000 -2.791950 5.431364`

To select elements from a vector we use squared parentheses. For example, to retrieve the second element the following code is used, where inside the parentheses the position of the element to be select is specified:

`2] y[`

`## [1] 99`

To select more than one element, a vector of positions is provided:

`c(1,4)] #select the first and forth element y[`

`## [1] -1.5 6.7`

It is also possible to replace values (after having selected them) by using the assignment operator `=`

:

```
# select the second element and replace it with 4
2] = 4
y[ y
```

`## [1] -1.50000 4.00000 2.70805 6.70000`

It is also possible to summarize the values in a vector by using **summary statistics** function such as the `sum`

or the `mean`

applied to `y`

or any function of it:

`sum(y) #sum of the elements of y`

`## [1] 11.90805`

`mean(y) #mean of the values of y`

`## [1] 2.977013`

`sum(y) / length(y) #another way for computing the mean`

`## [1] 2.977013`

`mean((y^3)+4) #first the operation ^3+4 is computed and then the sum`

`## [1] 99.3119`

There are also other summary functions such as, for example, `median`

, `quantile`

, `min`

and `max`

.

It is also possible to compute **logical operation** whose result is TRUE if the condition is met or FALSE otherwise. For example:

`>= 0 z `

`## [1] TRUE FALSE TRUE TRUE`

gives a vector of TRUE/FALSE according to the condition ‘bigger than or equal to 0’ applied to each element of `y`

. Summary statistics can be applied also to vector of logical values, in this case TRUE is considered as 1 and FALSE as 0.

`sum(z >= 0) #number of values >= 0 `

`## [1] 3`

`mean(z >= 0) #proportion of values >= 0`

`## [1] 0.75`

`mean(z >= 0)*100 #% of values >= 0`

`## [1] 75`

The following table lists all the logical operators available in R.

Operator in R | Description |
---|---|

<= >= | lower/bigger than or equal |

< > | lower/bigger |

== | exactly equal to |

!= | different from |

& | intersection (and) |

`|` (vertical bar) |
union (or) |

By using logical operator it is also possible to perform a selection of the elements satisfying a particular condition (for example `>=0`

). These values can be then saved in a new object:

```
= z[z > 0]
z2 z2
```

`## [1] 6.890000 5.500000 1.268636`

`length(z2)`

`## [1] 3`

Finally, by using logical operator it is also possible to select/replace elements in a vector by setting a condition. In this case it is not necessary to specify the positions in the vector of the elements to be selected/replaced. R will consider only the elements for which the condition is met:

```
# substitute the positive numbers of z with 0
> 0] = 0
z[z z
```

`## [1] 0 -10 0 0`

## 3.3 Exercises Lab 1

### 3.3.1 Exercise 1

- Compute \(\exp(3-\frac{4}{5})+\frac{\sqrt{3+2^5}}{4-7\cdot \log(10))}\)
- Create the vector named
`x`

which contains the following values \((10, log(0.2), 6/7, exp(4), sqrt(54), -0.124)\):

- Find the length of
`x`

. - Extract the elements of
`x`

which are bigger than 0 (0 included). - Extract the elements of
`x`

which are between 0 (included) AND 1 (excluded)? Hint: this means you have to consider numbers which are >= 0 AND <1 (double condition); the AND operator is given by`&`

. - Which elements of
`x`

are negative? Substitute them with 0. - Extract from
`x`

the 2nd and 4th value and save them in a new vector named`y`

. Compute \(y+sqrt(exp(-0.4))\).

### 3.3.2 Exercise 2

- Comment each line of code explaining which operation is executed:

```
= c(3,6,8)
x
x/2
x^2
xsqrt(x)
sum(x)
```

- Comment each line of code explaining which operation is executed:

```
2]
x[c(1,3)]
x[-3]
x[= c(2,5,1)
y
y-y
x*y
x>1.5]
x[y<= 6] y[x
```

### 3.3.3 Exercise 3

Simulate \(n=100\) values from the standard Normal distribution using the following code that will be explained in the following labs. The values are saved in an object named `x`

:

```
set.seed(123)
= rnorm(100)
x x
```

```
## [1] -0.560475647 -0.230177489 1.558708314 0.070508391 0.129287735
## [6] 1.715064987 0.460916206 -1.265061235 -0.686852852 -0.445661970
## [11] 1.224081797 0.359813827 0.400771451 0.110682716 -0.555841135
## [16] 1.786913137 0.497850478 -1.966617157 0.701355902 -0.472791408
## [21] -1.067823706 -0.217974915 -1.026004448 -0.728891229 -0.625039268
## [26] -1.686693311 0.837787044 0.153373118 -1.138136937 1.253814921
## [31] 0.426464221 -0.295071483 0.895125661 0.878133488 0.821581082
## [36] 0.688640254 0.553917654 -0.061911711 -0.305962664 -0.380471001
## [41] -0.694706979 -0.207917278 -1.265396352 2.168955965 1.207961998
## [46] -1.123108583 -0.402884835 -0.466655354 0.779965118 -0.083369066
## [51] 0.253318514 -0.028546755 -0.042870457 1.368602284 -0.225770986
## [56] 1.516470604 -1.548752804 0.584613750 0.123854244 0.215941569
## [61] 0.379639483 -0.502323453 -0.333207384 -1.018575383 -1.071791226
## [66] 0.303528641 0.448209779 0.053004227 0.922267468 2.050084686
## [71] -0.491031166 -2.309168876 1.005738524 -0.709200763 -0.688008616
## [76] 1.025571370 -0.284773007 -1.220717712 0.181303480 -0.138891362
## [81] 0.005764186 0.385280401 -0.370660032 0.644376549 -0.220486562
## [86] 0.331781964 1.096839013 0.435181491 -0.325931586 1.148807618
## [91] 0.993503856 0.548396960 0.238731735 -0.627906076 1.360652449
## [96] -0.600259587 2.187332993 1.532610626 -0.235700359 -1.026420900
```

- Compute the min, the max, the median and the mean of
`x`

. - How many values (in absolute and percentage terms) of
`x`

are lower than 0? - How many value (in absolute and percentage terms) are between -1 and +1?
- How many value (in absolute and percentage terms) are lower than -1 or bigger than +1?
- Replace each value of
`x`

lower than or equal to 0 with the corresponding absolute value (hint:`abs`

is the function for the absolute value). - Compute
`x/4-2`

and assign the values to a new vector named`y`

.