# Chapter 4 Sessions

## 4.1 Session 0

**1. Install R and R studio**

Install pre-compiled binary of R for your operating system: https://cloud.r-project.org

Install Preview version RStudio Desktop: https://www.rstudio.com/products/rstudio/download/preview/

If you have a pre-existing installation of R and/or RStudio, it is highly recommend that you reinstall both. You will face more dificulties if you run an old softwared version. If you upgrade R, you will need to update any packages you have installed.

**2. Test it**

Launch RStudio, you should see something simmilar to this but a bit emptier as you have not written anythign yet. Put your cursor in the pane labelled Console, which is where you interact with the live R process. In console, write something like: 2 + 1 and press return and you should get a 3. If you get a 3, you’ve succeeded in the installation of R and RStudio, congrats!

**Some extra resources:**

https://cran.r-project.org/doc/manuals/R-admin.html

https://cran.r-project.org/doc/FAQ/R-FAQ.html#What-is-R_003f

## 4.2 Session 1

### 4.2.1 Learning objectives

- Learn what is programming
- Learn what is R
- Learn the basics of RStudio
- Learn the basics of R:
- Variable assigment
- Basic data types
- Vectors
- Data frames

### 4.2.2 Contents

**Basics of R:**

- Program
- Language: Not compiled, simple sintaxys
- R “flow”:
- Variables, data, functions, results, etc, are stored in the active memory of the computer in the form of objects which have a name.
- You can do actions on these objects with operators (arithmetic, logical, comparison, . . .) and functions (which are themselves objects).

**Practice**

- Using Rstudio
- Open a project
- Organize your folder (rstudio projet / Code / Data / Figures / )
**Super tip of the day!** - Console
- Files
- Script
- Keyboard shortcuts

- Variable assigment: the “assign” operator
- concept overwiting a variable (s in the active memory, not the data on the disk)
- Note that R is case sensitive!
- “#” is used for comments

- Basic data types
- Decimals values like 4.5 are called numerics.
- Natural numbers like 4 are called integers. Integers are also numerics.
- Boolean values (TRUE or FALSE) are called logical.
- Text (or string) values are called characters. The quotation marks indicate that is a character.

- Vectors
- create vectors
- name a vector
- select elements from the vector
- compare different vectors
- combine vectors

- Data frames
- creating a data frame
- quick look to the data frame: structure, rownames, number of columns, number of rows, summary,
- select data fram elements
- subset in a data frame
- ordering
- sorting

**The code of this session** can be found here

## 4.3 Session 2

### 4.3.1 Learning objectives

- Learn the basics of R:
- Variable assigment (review)
- Basic data types (review)
- Vectors (review)
- Data frames (review & new)
- Lists
- Matrices
- Factors

- Basic math functions
- Basic visualizations of the data
- Basic statistics
- Using Libraries
- File path
- Working directory

### 4.3.2 Contents

- Data frames
- Understanding a data frame
- Count rows and columns
- Add rows and columns
- Subset a data frame
- list of variables
- name of the columns
- Vector functions (sort, counts of values, unique values)

- Lists
- Lists, as opposed to vectors, can hold components of different types
- create a list
- list subsetting
- name lists

- Matrices
- create matrices and to understand how you can do basic computations with them.

- Factors
- create, subset and compare

- Maths functions
- Maximun value
- Minimun value
- Mean value
- Median
- Variance
- Standart deviation
- Correlation
- Round values

- Basic visualizations of the data
- Ploting
- histograms

- Basic statistics
- linear model
- summary of the linear model

- Using Libraries
- File path
- Working directory