Conditions and If Statements
You already know that R supports familiar comparison conditions from mathematics, such as:
| Operator | Name | Example | Try it |
|---|---|---|---|
| == | Equal | x == y | |
| != | Not equal | x != y | |
| > | Greater than | x > y | |
| < | Less than | x < y | |
| >= | Greater than or equal to | x >= y | |
| <= | Less than or equal to | x <= y |
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")
}
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")
}
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")
}
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")
}