class: center, middle, inverse, title-slide # Lecture 1 - Introduction to R ### Ali Seyhun Saral
ali.saral@unibo.it
### 08 November 2021 --- # RStudio Interface <span class="border"> <img src="./img/03-rconsole_detail.png" width="100%" style="display: block; margin: auto;" /> </span> --- # Introduction to R Programming The most simple way to interact with R is to use it as a calculator. If you go ahead and write `2 + 2` to the console and press enter you will get as the output: ``` ## [1] 4 ``` --- ## Arithmetic Operators You can use these **arithmetic operators** to carry out calculations: -- - `+` addition ```r 9 + 2 ``` ``` ## [1] 11 ``` -- - `-` subtraction ```r 9 - 2 ``` ``` ## [1] 7 ``` -- - `*` multiplication ```r 9 * 2 ``` ``` ## [1] 18 ``` --- - `/` division ```r 9 / 2 ``` ``` ## [1] 4.5 ``` -- - `^` exponent ```r 9 ^ 2 ``` ``` ## [1] 81 ``` -- - `%%` modulus ```r 9 %% 2 ``` ``` ## [1] 1 ``` --- ## What is an operator? - A special symbol(s) to do a perform a specific operation. -- - Like functions but usually doing the essential bits of a language. -- - Usually take values on either side of them and return an output. --- - They are really simple. - **Arithmetic operators**: calculate things - **Logical operators**: logical questions/operations - **Relational operators**: Compare things --- ## Logical Operators `R` have `TRUE` and `FALSE` values built in. The usual logical rules apply to manipulate and combine logical values. --- - `!` negation (NOT) -- ```r ! TRUE ``` ``` ## [1] FALSE ``` --- - `&` element-wise AND -- ```r TRUE & TRUE ``` ``` ## [1] TRUE ``` -- ```r TRUE & FALSE ``` ``` ## [1] FALSE ``` -- ```r FALSE & FALSE ``` ``` ## [1] FALSE ``` --- - `*` element-wise OR ```r TRUE | TRUE ``` ``` ## [1] TRUE ``` -- ```r TRUE | FALSE ``` ``` ## [1] TRUE ``` -- ```r FALSE | FALSE ``` ``` ## [1] FALSE ``` --- ## Relational Operators Relational operators are used to compare the values. - `==` Equal to Checks whether two values are equal. (Not to be confused with `=` which might be used for variable assignment. We will talk about that) ```r 2 == 9 ``` ``` ## [1] FALSE ``` ```r 9 == 2 ``` ``` ## [1] FALSE ``` ```r 3 == 3 ``` ``` ## [1] TRUE ``` --- - `==` Equal to Checks whether two values are NOT equal. (Mind the single `=`) ```r 2 != 9 ``` ``` ## [1] TRUE ``` ```r 9 != 2 ``` ``` ## [1] TRUE ``` ```r 3 != 3 ``` ``` ## [1] FALSE ``` --- - `<` less than ```r 2 < 9 ``` ``` ## [1] TRUE ``` ```r 9 < 2 ``` ``` ## [1] FALSE ``` ```r 3 < 3 ``` ``` ## [1] FALSE ``` --- - `<=` less than or equal to ```r 2 <= 9 ``` ``` ## [1] TRUE ``` ```r 9 <= 2 ``` ``` ## [1] FALSE ``` ```r 3 <= 3 ``` ``` ## [1] TRUE ``` --- - `>` greater than ```r 2 > 9 ``` ``` ## [1] FALSE ``` ```r 9 > 2 ``` ``` ## [1] TRUE ``` ```r 3 > 3 ``` ``` ## [1] FALSE ``` --- - `>` greater than or equal to ```r 2 >= 9 ``` ``` ## [1] FALSE ``` ```r 9 >= 2 ``` ``` ## [1] TRUE ``` ```r 3 >= 3 ``` ``` ## [1] TRUE ``` --- # Variables - Usually our problems are generalizable -- - We don't write numbers and values to begin with -- - We often would like to save the output somewhere and use that value --- # Variables - Variables are **names** to give certain values. -- - Variable's name represent what information variable contains -- - We use the notation `<-` to assign a value to a variable. -- ```r ali <- 34 piero <- 24 agetotal <- ali + piero ``` -- ```r print(agetotal) ``` ``` ## [1] 58 ``` --- # `print()` `print()` function is used to write the contents of variables (or other objects) into our console. ```r print(44) ``` ``` ## [1] 44 ``` -- ```r ali = 11 * 3 print(ali) ``` ``` ## [1] 33 ``` -- You can print things on the fly ```r print(4 * 10 + 2) ``` ``` ## [1] 42 ``` --- ## Functions ### Built-in functions `R` has many built in functions that are helpful for making calculations. Functions have the form: ``` functionname(parameter) ``` --- Some examples: - `abs(x)` absolute value ```r abs(9) ``` ``` ## [1] 9 ``` ```r abs(-9) ``` ``` ## [1] 9 ``` --- - `sqrt(x)` square root ```r sqrt(9) ``` ``` ## [1] 3 ``` ```r sqrt(10) ``` ``` ## [1] 3.162278 ``` --- ## Creating user-defined functions You can write your own functions in R. To define your own functions, you need a **name** for the function, a parameter or several parameters, and the **recipe** of what a function does. -- ```r square_root_and_multiply_by_2 <- function(x) { return(sqrt(x) * 2) } square_root_and_multiply_by_2(9) ``` ``` ## [1] 6 ``` --- ### Example ```r # Writing our own functions: P <- function(x, y) { return(x > y) } P(4,3) ``` ``` ## [1] TRUE ``` ```r P(-1,4) ``` ``` ## [1] FALSE ``` --- ```r Q <- function(x,y,z) { return(x^2 + y^2 == z^2) } Q(3,4,5) ``` ``` ## [1] TRUE ``` ```r Q(2,2,3) ``` ``` ## [1] FALSE ``` --- ## Excercises