class: center, middle, inverse, title-slide .title[ # R through the user interface of RStudio - performing basic operations ] .subtitle[ ## Learning the basics of R - Part 1 ] .author[ ### Ernest Guevarra ] .date[ ### 23 October 2024 ] --- # Outline 1. Using RStudio to access the R console 2. Some basic operators in R * Arithmetic * Relational * Logical * Assignment 3. Using RStudio to create scripts --- # Using RStudio to access the R console * RStudio has a specific window/pane for the R console which behaves exactly the same as the raw R console * Issue commands directly on the console to produce a desired outcome or perform a specific action * Most commands produce an output that is shown on the console --- class: inverse, center, middle # Basic operators in R --- # Arithmetic operators These operators are used to carry out mathematical operations like addition and multiplication. Here is a list of arithmetic operators available in R. <table class="table table-striped" style="width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Operator </th> <th style="text-align:left;"> Description </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> + </td> <td style="text-align:left;"> Addition </td> </tr> <tr> <td style="text-align:left;"> - </td> <td style="text-align:left;"> Subtraction </td> </tr> <tr> <td style="text-align:left;"> * </td> <td style="text-align:left;"> Multiplication </td> </tr> <tr> <td style="text-align:left;"> / </td> <td style="text-align:left;"> Division </td> </tr> <tr> <td style="text-align:left;"> ^ </td> <td style="text-align:left;"> Exponent </td> </tr> <tr> <td style="text-align:left;"> %% </td> <td style="text-align:left;"> Modulus </td> </tr> <tr> <td style="text-align:left;"> %/% </td> <td style="text-align:left;"> Integer Division </td> </tr> </tbody> </table> --- # Arithmetic operators - application Let us try R's arithmetic operations to calculate BMI: $$ \text{BMI} ~ = ~ \frac{kgs}{m ^ 2} $$ using the following values: $$ \text{weight} ~ = ~ 80 ~ \text{kgs} $$ $$ \text{height} ~ = ~ 1.6 ~ \text{metres} $$ --- # Arithmetic operators - application ``` r 80 / 1.6 ^ 2 ``` ``` ## [1] 31.25 ``` --- # Relational operators Relational operators are used to compare between values. Here is a list of relational operators available in R. <table class="table table-striped" style="width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Operator </th> <th style="text-align:left;"> Description </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> < </td> <td style="text-align:left;"> Less than </td> </tr> <tr> <td style="text-align:left;"> > </td> <td style="text-align:left;"> Greater than </td> </tr> <tr> <td style="text-align:left;"> <= </td> <td style="text-align:left;"> Less than or equal to </td> </tr> <tr> <td style="text-align:left;"> >= </td> <td style="text-align:left;"> Greater than or equal to </td> </tr> <tr> <td style="text-align:left;"> == </td> <td style="text-align:left;"> Equal to </td> </tr> <tr> <td style="text-align:left;"> != </td> <td style="text-align:left;"> Not equal to </td> </tr> </tbody> </table> --- # Relational operators - application Let us apply the relational operators using again BMI as an example. Say we have **Person A** with a weight of *80 kilograms* and a *height of 1.6 metres* and **Person B** with a weight of *120 kilograms* and a height of *210 centimetres*. In R, try to answer the following questions: 1. Is **Person A** heavier in weight compared to **Person B**? 2. Is **Person A** taller in height compared to **Person B**? 3. Whose BMI is greater, **Person A** or **Person B**? --- # Relational operators - application ### 1. Is **Person A** heavier in weight compared to **Person B**? ``` r 80 > 120 ## Is Person A's weight greater than Person B's weight ``` ``` ## [1] FALSE ``` ``` r 80 < 120 ## Is Person A's weight lesser than Person B's weight ``` ``` ## [1] TRUE ``` ``` r 80 == 120 ## Is Person A's weight the same as Person B's weight ``` ``` ## [1] FALSE ``` --- # Relational operators - application ### 2. Is **Person A** taller in height compared to **Person B**? ``` r 1.6 > 210 / 100 ## Is Person A's height greater than Person B's height ``` ``` ## [1] FALSE ``` ``` r 1.6 < 210 / 100 ## Is Person A's height lesser than Person B's height ``` ``` ## [1] TRUE ``` ``` r 1.6 == 210 / 100 ## Is Person A's height the same as Person B's height ``` ``` ## [1] FALSE ``` --- # Relational operators - application ### 3. Whose BMI is greater, **Person A** or **Person B**? ``` r 80 / 1.6 ^ 2 > 120 / (210 / 100) ^ 2 ## Is Person A's BMI greater than Person B's BMI ``` ``` ## [1] TRUE ``` ``` r 80 / 1.6 ^ 2 < 120 / (210 / 100) ^ 2 ## Is Person A's BMI lesser than Person B's BMI ``` ``` ## [1] FALSE ``` ``` r 80 / 1.6 ^ 2 == 120 / (210 / 100) ^ 2 ## Is Person A's BMI the same as Person B's BMI ``` ``` ## [1] FALSE ``` --- # Logical operators Logical operators are used to carry out Boolean operations like AND, OR etc. Operator | Description :--- | :--- ! | Logical NOT & | Element-wise logical AND && | Logical AND | | Element-wise logical OR || | Logical OR --- # Logical operators - application Let us apply the logical operators again using the example of BMI for Person A and Person B in the previous exercise. In R, answer the following questions using logical operators: 1. Is the weight of **Person A** AND the weight of **Person B** both equal to 80 kilograms? 2. Is the weight of **Person A** OR the weight of **Person B** less than 100 kilograms? 3. Is the weight of **Person A** greater than the weight of **Person B** AND the height of **Person A** greater than the height of **Person B**? 4. Is the weight of **Person A** greater than the weight of **Person B** OR the height of **Person A** greater than the height of **Person B**? --- # Logical operators - application ### 1. Is the weight of **Person A** AND the weight of **Person B** both equal to 80 kilograms? ``` r 80 == 80 & 120 == 80 ``` ``` ## [1] FALSE ``` ### 2. Is the weight of **Person A** OR the weight of **Person B** less than 100 kilograms? ``` r 80 < 100 | 120 < 100 ``` ``` ## [1] TRUE ``` --- # Logical operators - application ### 3. Is the weight of **Person A** greater than the weight of **Person B** AND the height of **Person A** greater than the height of **Person B**? ``` r 80 > 120 & 1.6 > 2.1 ``` ``` ## [1] FALSE ``` ### 4. Is the weight of **Person A** greater than the weight of **Person B** OR the height of **Person A** greater than the height of **Person B**? ``` r 80 > 120 | 1.6 > 2.1 ``` ``` ## [1] FALSE ``` --- # Assignment operators These operators are used to assign values to objects. <table class="table table-striped" style="width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Operator </th> <th style="text-align:left;"> Description </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> <- </td> <td style="text-align:left;"> Leftwards assignment </td> </tr> <tr> <td style="text-align:left;"> <<- </td> <td style="text-align:left;"> Leftwards assignment </td> </tr> <tr> <td style="text-align:left;"> = </td> <td style="text-align:left;"> Leftwards assignment </td> </tr> <tr> <td style="text-align:left;"> -> </td> <td style="text-align:left;"> Rightwards assignment </td> </tr> <tr> <td style="text-align:left;"> ->> </td> <td style="text-align:left;"> Rightwards assignment </td> </tr> </tbody> </table> --- # Assignment operators - application Let us again use the BMI example to apply the assignment operators: 1. Assign the weight of person A to an object named `weight_a` 2. Assign the height of person A to an object named `height_a` 3. Calculate BMI for person A using objects `weight_a` and `height_a`. Assign the value of BMI to an object named `bmi_a`. --- # Assignment operators - application ### 1. Assign the weight of person A to an object named `weight_a` ``` r weight_a <- 80 weight_a ``` ``` ## [1] 80 ``` ### 2. Assign the height of person A to an object named `height_a` ``` r height_a <- 1.6 height_a ``` ``` ## [1] 1.6 ``` --- # Assignment operators - application ### 3. Calculate BMI for person A using objects `weight_a` and `height_a`. Assign the value of BMI to an object named `bmi_a`. ``` r bmi_a <- weight_a / height_a ^ 2 bmi_a ``` ``` ## [1] 31.25 ``` --- class: inverse, center, middle # Questions? --- class: inverse, center, middle # Practical session Using what we have learned in the first half of the session, we'll learn about how to write R scripts. --- # Using RStudio to create scripts * So far, we have tried issuing commands in R straight into the console to perform single commands at a time * In real life context, we will rarely use R for a single command. To make meaningful analysis, we will often string together a series of commands to produce an intended result/output * We will also often have to repeat the same commands with different data or parameters * As such, direct to console issuing of commands in R will be highly inefficient --- # Using RStudio to create scripts .pull-left[ RStudio, being an **integrated development environment (IDE)**, provides functionality and tools for * recording multiple lines of commands which can be run/issued onto the console line by line; and, * saving the recorded multiple lines of code/commands for later use. This record of multiple lines of code/commands is often called an **R script** and is saved as plain text file with a `.R` extension. ] .pull-right[ ``` r ## R script to calculate BMI of ## person A and person B weight_a <- 80 height_a <- 1.6 bmi_a <- weight_a / height_a ^ 2 weight_b <- 12 height_b <- 2.1 bmi_b <- weight_b / height_b ^ 2 ``` ] --- class: inverse, center, middle # Questions? --- class: inverse, center, middle # Thank you! Slides can be viewed at https://oxford-ihtm.io/open-reproducible-science/session2.html PDF version of slides can be downloaded at https://oxford-ihtm.io/open-reproducible-science/pdf/session2-r-basics-part1.pdf R scripts for slides available [here](https://github.com/OxfordIHTM/open-reproducible-science/blob/main/session2.Rmd)