R If ... Else


Conditions and If Statements

R supports the usual logical conditions from mathematics:

Operator Name Example Try it
== Equal x == y Try it »
!= Not equal x != y Try it »
> Greater than x > y Try it »
< Less than x < y Try it »
>= Greater than or equal to x >= y Try it »
<= Less than or equal to x <= y Try it »

These conditions can be used in several ways, most commonly in "if statements" and loops.


The if Statement

An "if statement" is written with the if keyword, and it is used to specify a block of code to be executed if a condition is TRUE:

Example

a <- 33
b <- 200

if (b > a) {
  print("b is greater than a")
}
Try it Yourself »

In this example we use two variables, a and b, which are used as a part of the if statement to test whether b is greater than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so we print to screen that "b is greater than a".

R uses curly brackets { } to define the scope in the code.


Else If

The else if keyword is R's way of saying "if the previous conditions were not true, then try this condition":

Example

a <- 33
b <- 33

if (b > a) {
  print("b is greater than a")
} else if (a == b) {
  print ("a and b are equal")
}
Try it Yourself »

In this example a is equal to b, so the first condition is not true, but the else if condition is true, so we print to screen that "a and b are equal".

You can use as many else if statements as you want in R.


If Else

The else keyword catches anything which isn't caught by the preceding conditions:

Example

a <- 200
b <- 33

if (b > a) {
  print("b is greater than a")
} else if (a == b) {
  print("a and b are equal")
} else {
  print("a is greater than b")
}
Try it Yourself »

In this example, a is greater than b, so the first condition is not true, also the else if condition is not true, so we go to the else condition and print to screen that "a is greater than b".

You can also use else without else if:

Example

a <- 200
b <- 33

if (b > a) {
  print("b is greater than a")
} else {
  print("b is not greater than a")
}
Try it Yourself »


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.