Control Flow

Anouncements

Quarto Template for HW

Download it here: Github Repo

OR Type this in the RStudio terminal:

quarto use template inqs909/m408_hw

Type Y for the trust author. Type a name of a new directory in where to save the file. For example, type hw1.

OR Save this in an empty source quarto document:

---
title: "Title"
author: "Name Here"
date: "`r format(Sys.time(),'%m-%d-%Y')`"
format: 
  html:
    toc: true
    toc-depth: 2
    code-fold: true
    code-tools: true
    code-line-numbers: true
knitr:
  opts_chunk:
    echo: true
    message: false
    warning: false
    error: true
    tidy: styler
    R.options:
      digits: 3
      max.print: 100
---

## Problem 1

## Problem 2

## Problem 3

Guides:

Learning Objectives

  • Control Flow

  • Indexing

  • Comparing Numbers

  • if/else Statements

  • try()

  • for Statements

Control Flow

The order a computer will complete tasks.

Usually incorporates statements and loops.

Indexing

  • Indexing

  • Comparing Numbers

  • if/else Statements

  • try()

  • for Loops

Indexing

Within an R object, you can access an element by indexing it.

Indexing tells R which values to output.

Vectors

A vector can be indexed by adding [] after the object’s name and specifying the number of each element.

Matrices

A matrix can be indexed by adding [] after the object’s name and specifying the number of each element. Separate the values by commas for specific indexes.

Data Frames

Data frames can be indexed using the $ operator and [].

Lists

Lists can be indexed using the [[]] for a specific element of a list.

Comparing Numbers

  • Indexing

  • Comparing Numbers

  • if/else Statements

  • try()

  • for Loops

Comparing Numbers

You can compare two numbers, or objects, that will result in a logical output.

Comparing Numbers Operators

Operator Description
> Greater Than
< Less Than
>= Greater than or equal
<= Less than or equal
== Equals
!= Not Equals

Comparing Vectors

When you compare a number to a vector, it will result as a logical vector.

Example

Try the following code and explain what is happening:

if/else Statements

  • Indexing

  • Comparing Numbers

  • if/else Statements

  • try()

  • for Loops

if/else Statements

if/else statements are used to conduct specific tasks depending on the conditions

if Statement

An if statement is used to if you want R to perform a specific function if a certain condition is met. An if statement will only run a task if a logical is returned. You will need type if, followed by the condition (as a logical) in parentheses, then the task.

Example

else statement

An else statement will conduct a different task if the if statement does not conduct the tasks.

Example

Chain if/else statement

If you have more than two options, you can chain if/else statements by adding an if statement immediately after the word else.

Example

try()

  • Indexing

  • Comparing Numbers

  • if/else Statements

  • try()

  • for Loops

try()

The try() is an extremely powerful function that will prevent a code from stopping if an error occurs.

Example

for Loops

  • Indexing

  • Comparing Numbers

  • if/else Statements

  • try()

  • for Loops

for Loops

for loops are used to conduct an iterative task with slight changes to the input. The general format goes as follows:

for (index in vector){
  Conduct task
}

You will repeat the for loop untie all the elements in the vector have been used.

Example

Compute the mean:

\[ \bar x = \frac{1}{n}\sum^n_{i=1}x_i \]

x <- rnorm(100)
mean(x)
#> [1] 0.1095409

Example

Compute the variance:

\[ s^2 = \frac{1}{n-1}\sum^n_{i-1}(x_i-\bar x)^2 \]

x <- rnorm(100)
var(x)
#> [1] 0.6916603