If statements in R are used to define choice in the script. For example, of there are two choices ‘a’ and ‘b’ and an if statement is used. Then if a certain condition exist ‘a’ happens if not, ‘b’ happens.

Before we explore this more closely we need to setup a scenario and a function for it. Imagine that James wants to donate $40.00 for every point his team scores in a a game. To calculate this we create the following function.

CashDonate <- function(points, Dollars_per_point=40){ game.points<- points * Dollars_per_point round(game.points) }

Here is what we did

- We created the function “CashDonate”
- The function has the arguments ‘points’ and ‘Dollars_per_point’ which has a default value of 40
- The variable ‘game.points’ is created to hold the value of ‘points’ times ‘Dollars_per_point’
- The output of ‘game.points’ is then rounded which is the total amount of money that should be donated

The function works perfectly

Below is an example of the function working when James’ team scores 95 points

> CashDonate(95) [1] 3800

Later, James comes to you upset. His team is scoring so many points that it is starting to affect his budget. He now wants to change the amount he donates * IF *the team scores over 100 points. If his team scores 100 points or more James wants to donate $30.00 per point instead of $40.00 dollars. Below is the modified function. Notice the use of the if in the script.

CashDonate <- function(points, Dollars_per_point=40){ game.points<- points * Dollars_per_point if(points > 100) {game.points <-points * 30 } round(game.points) }

Most of the code is the same except notice the following changes

- We added the argument ‘if’ after this we put the condition
- If the number of points was greater than 100 we now would multiple the results of the variable ‘games.points’ by 30 instead of the default value of 40

Below are two examples. Example 1 shows the results of the modified ‘CashDonate’ function when less than 100 points are scored. Example two will show the results when more than 100 points are scored.

EXAMPLE 1 > CashDonate(98) [1] 3920 EXAMPLE 2 > CashDonate(103) [1] 3090

**Else Statements**

Else statements allows for the use of TRUE/FALSE statements. For example, if ‘a’ is true do this if not do something else. Below is a scenario that requires the use of an ‘else’ statement.

The owner of James’ team decides that he wants to contribute to the donating as well. He states that if it is a home game he will give 50% of whatever James give and he will give 30% of whatever James gives for away games. The total will be added together as one amount. Below is the modified code.

CashDonate <- function(points, Dollars_per_point=40, HomeGame=TRUE){ game.points<- points * Dollars_per_point if(points > 100) {game.points <-points * 30} if(HomeGame) {Total.Donation <- game.points * 1.5 } else {Total.Donation <- game.points * 1.3} round(Total.Donation) }

Here is an explanation

- At the top we add the argument “HomeGame” and set the default to “TRUE” which means the other choice is “FALSE” which is for an away game
- The rule for points over 100 is the same as before
- The second ‘if’ statement talks about the logical statement for “HomeGame”. If it is a home game the results of ‘game.points’ is multiplied by 1.5 or 50%. If it is not a home came (notice the ‘else’) than the results of ‘game.points’ is multiplied by 1.3 or 30%. The results of either choice is stored in the variable ‘Total.Donation’
- Lastly, the results of ‘Total.Donation’ are rounded

Below are 4 examples

Example 1 is less than 100 points scored at a home game

> CashDonate(98) [1] 5880

Example 2 is more than 100 points scored at a home game

> CashDonate(102) [1] 4590

Example 3 is less than 100 points scored at an away game

> CashDonate(99, HomeGame = FALSE) [1] 5148

Example 4 is more than 100 points scored at an away game

> CashDonate(104, HomeGame = FALSE) [1] 4056

**Conclusion**

If statements provide choice. Else statements provide choice for logical arguments. Both can be used in R to provide several different actions in a script.

Pingback: Logical Flow in R: If/Else Statements | Educati...

Pingback: Logical Flow in R: If/Else Statements Part II | educationalresearchtechniques

Pingback: Switch Function in R | educationalresearchtechniques