 How to Use If Statements in R - dummies

If statements can be very useful in R, as they are in any programming language,. Often, you want to make choices and take action dependent on a certain value.

Defining a choice in your code is pretty simple: If this condition is true, then carry out a certain task. Many programming languages let you do that with exactly those words: if . . . then. R makes it even easier: You can drop the word then and specify your choice in an if statement.

An if statement in R consists of three elements:

• The keyword if

• A single logical value between parentheses (or an expression that leads to a single logical value)

• A block of code between braces that has to be executed when the logical value is TRUE

Here is a very small function, priceCalculator(), that calculates the price you charge to a customer based on the hours of work you did for that customer. The function should take the number of hours (hours) and the price per hour (pph) as input. The priceCalculator() function could be something like this:

```priceCalculator <- function(hours, pph=40){
net.price <- hours * pph
round(net.price)
}```

Here’s what this code does:

• With the function keyword, you define the function.

• Everything between the braces is the body of the function (see Chapter 8).

• Between the parentheses, you specify the arguments hours (without a default value) and pph (with a default value of \$40 per hour).

• You calculate the net price by multiplying hours by pph.

• The outcome of the last statement in the body of your function is the returned value. In this case, this is the total price rounded to the dollar.

You could drop the argument pph and just multiply hours by 40. But that would mean that if, for example, your colleague uses a different hourly rate, he would have to change the value in the body of the function in order to be able to use it. It’s good coding practice to use arguments with default values for any value that can change. Doing so makes a function more flexible and usable.

Now imagine you have some big clients that give you a lot of work. To keep them happy, you decide to give them a reduction of 10 percent on the price per hour for orders that involve more than 100 hours of work. So, if the number of hours worked is larger than 100, you calculate the new price by multiplying the price by 0.9.

You can write that almost literally in your code like this:

```priceCalculator <- function(hours, pph=40){
net.price <- hours * pph
if(hours > 100) {
net.price <- net.price * 0.9
}
round(net.price)
}```

Copy this code in a script file, and send it to the console to make it available for use. If you try out this function, you can see that the reduction is given only when the number of hours is larger than 100:

```> priceCalculator(hours = 55)
 2200
> priceCalculator(hours = 110)
 3960```

This construct is the most general way you can specify an if statement. But if you have only one short line of code in the code block, you don’t have to put braces around it. You can change the complete if statement in the function with the following line:

`if(hours > 100) net.price <- net.price * 0.9`

The usual way of getting help on a function named, for example, fun.name (?fun.name) does not work for if. To access the built-in help for if, you have to quote the function name. You can use single quotes, double quotes, or backticks. Each of the following statements takes you to the Help page for if:

```?'if'
?"if"
?`if````