Similar to other pro­gram­ming languages, if-else state­ments are also com­mon­place when pro­gram­ming in R. With if-else state­ments, you can formulate different con­di­tions and guide the execution of a program.

What are possible uses for if-else state­ments in R?

An if-else statement, which is often referred to as con­di­tion­al branching, ensures that certain in­struc­tions are only carried out if a condition is met. The con­di­tion­al func­tion­al­ity of if-else state­ments is fun­da­ment­al to pro­gram­ming, which is why it is hard to imagine using most programs without them. This is es­pe­cially true when con­struct­ing complex al­gorithms.

Using if-else state­ments in your source code is con­veni­ent if you want to check user input against a specific value. The most common use of R if-else struc­tures, however, is in con­junc­tion with for-loop state­ments in R. If you are looking for a specific value from a data structure, you can use an if statement to exit the loop once you have found it.

Tip

If you want to keep your projects and programs in a secure place, Webspace from IONOS is a great option. Automatic backups prevent data loss, ensuring that you have access to your data whenever you need it.

What is the syntax for if-else state­ments in R?

You can use if-else state­ments in R in a variety of ways. Re­gard­less of how you combine the keywords ‘if’, ‘else’ and ‘else if’, the basic logic remains the same: The execution of an in­di­vidu­al code block is linked to a condition.

The syntax for this command is strictly defined. The keyword ‘if’ is always followed by a condition, which is specified in par­en­theses. Arith­met­ic or logical com­par­is­on operators are often used within the condition. This is followed by a block of code in curly braces that is only executed if the condition you specify is true. Depending on the goal of your con­di­tion­al statement, you can now use the ‘else’ keyword to introduce a block of code that will be executed if the condition stated after ‘if’ is not met. You also have the option of using the ‘else if’ keyword to create ad­di­tion­al con­di­tions.

Note

In R, if there is only one line of code that you want to execute, you can omit the curly braces from the if-else statement. This can help to improve read­ab­il­ity. However, for beginners who are still learning how to program, it is advisable to use the curly braces even if it’s not necessary. That way you don’t have to worry about anything going wrong.

R-if state­ments

An if statement without else can be used in instances where the program should only generate a response when the condition is fulfilled. If the condition is not met, the program will continue along its standard path of execution. Here is an example of an if statement in R:

a <- 0
b <- 40
if (a == 0) {
    print("Zero division not allowed")
stop()
}
c <- b / a
R

In the above example, the number stored in variable b is to be divided by the number in variable a. Since division by 0 is not allowed, the if condition checks whether the value stored in ‘a’ is 0. If this condition is true, the program will execute the code in curly brackets. A response is issued stating that zero division is not allowed, and the cal­cu­la­tion is stopped. If the condition is false, the program will not be in­ter­rup­ted, con­tinu­ing instead along its standard path.

If-else state­ments in R

If you want the program to respond to an un­ful­filled condition as well, you can use an if-else statement. You’ll first want to formulate an if condition like the one in the example above. If the condition in the if statement is not met, the program will skip over the cor­res­pond­ing block of code and instead execute the code in the curly brackets following the word ‘else’. Here is an example to help il­lus­trate this:

a <- 4
b <- 2
if (b < a) {
    print("a is greater than b")
}
else {
    print("a and b are equal or b is greater than a")
}
R

Again, two variables (a and b) are created, both of which contain integer values. The if condition checks if value b is smaller than value a. If it is, the in­struc­tions inside the curly brackets will be executed. If value be is not smaller than value a, the program jumps directly to the statement block after the keyword ‘else’ and executes the lines of code that cor­res­pond to ‘else’.

Else-if state­ments in R

In some cases, you may want to check multiple con­di­tions. For this, you can use the ‘else if’ keyword. If you are familiar with if-else state­ments in Python, you may already know the else-if statement from R as the keyword ‘elif’.

a <- 4
b <- 2
if (b < a) {
    print("a is greater than b")
}
else if (b > a) {
    print("b is greater than a")
}
R

This code example is very similar to the previous one. There are a couple of dif­fer­ences though. In this example, the else statement has been replaced with an else if statement. Fur­ther­more, it’s possible to specify what the al­tern­at­ive to the previous condition is.

It’s important to note that the con­di­tions specified with ‘else if’ are mutually exclusive. If, as in our case, the first condition is already true, the code in­tro­duced with ‘else if’ will not be executed at all. Compared to creating several simple if state­ments, employing else if state­ments can help save computing power.

Go to Main Menu