Often, you need to execute some statements only when some condition is met. You can use following conditional statements in your code to do this.
if
Statement: use it to execute a block of code, if a specified condition is trueelse
Statement: use it to execute a block of code, if the same condition is falseelse if
Statement: use it to specify a new condition to test, if the first condition is falseifelse()
Function: use it when to check the condition for every element of a vector
The if Statement
Use if
statement to execute a block of code, if the condition is true.
Syntax
Making a simple comparison
x <- 7
y <- 5
if(x > y) {
print("x is greater")
}
[1] "x is greater"
Likewise, you can use following comparison operators to compare two values:
Operator | Meaning | Example |
== | Equals | if (x == y) |
!= | Not equals | if (x != y) |
> | Greater than | if (x > y) |
>= | Greater than or equal to | if (x >= y) |
< | Less than | if (x < y) |
<= | Less than or equal to | if (x <= y) |
More Examples
In R, any non-zero value is considered TRUE, whereas a zero is considered FALSE. That’s why all the below if statements are valid.
# mathematical expression
x <- 7
y <- 5
if(x + y) {
print("True")
}
[1] "True"
# any non-zero value
if(-3) {
print("True")
}
[1] "True"
if Statement Without Curly Braces
If you have only one statement to execute, you can skip curly braces.
x <- 7
y <- 5
if(x > y) print("x is greater")
[1] "x is greater"
Nested if Statement
You can write one if
statement inside another if
statement to test more than one condition and return different results.
x <- 7
y <- 5
z <- 2
if(x > y) {
print("x is greater than y")
if(x > z) print("x is greater than y and z")
}
[1] "x is greater than y"
[1] "x is greater than y and z"
The else Statement
Use else
statement to execute a block of code, if the condition is false.
Syntax
condition: is any expression that evaluates to either true or false.
if statement: specifies a block of statements if the condition is true.
else statement: specifies a block of statements if the condition is false.
A Simple if-else comparison
x <- 7
y <- 5
if(x > y) {
print("x is greater")
} else {
print("y is greater")
}
[1] "x is greater"
The else if Statement
Use else if
statement to specify a new condition to test, if the first condition is false.
Syntax
condition: is any expression that evaluates to either true or false.
if statement: specifies a block of statements if the condition is true.
elif statement: specifies a new condition to test, if the first condition is false.
else statement: specifies a block of statements if the condition is false.
Using else-if Statement
x <- 5
y <- 5
if(x > y) {
print("x is greater")
} else if(x < y) {
print("y is greater")
} else {
print("x and y are equal")
}
[1] "x and y are equal"
In R, you can use as many else if
statements as you want in your program. There’s no limit. However, it’s not a best practice when you want to make series of decisions. You can use switch() function as an efficient way.
Multiple Conditions
To join two or more conditions into a single if
statement, use logical operators viz. &&
(and), ||
(or) and !
(not).
&&
(and) expression is True, if all the conditions are true.
x <- 7
y <- 5
z <- 2
if(x > y && x > z) {
print("x is greater")
}
[1] "x is greater"
||
(or) expression is True, if at least one of the conditions is True.
x <- 7
y <- 5
z <- 9
if(x > y || x > z) {
print("x is greater than y or z")
}
[1] "x is greater than y or z"
!
(not) expression is True, if the condition is false.
x <- 7
y <- 5
if(!(x < y)) {
print("x is greater")
}
[1] "x is greater"
One Line If…Else
If you have only one statement to execute, one for if , and one for else , you can put it all on the same line:
Syntax
Examples
x <- 7
y <- 5
if (x > y) print("x is greater") else print("y is greater")
[1] "x is greater"
You can also use it to select variable assignment.
x <- 7
y <- 5
max <- if (x > y) x else y
max
[1] 7
The ifelse() Function
In R, conditional statements are not vector operations. They deal only with a single value.
If you pass in, for example, a vector, the if
statement will only check the very first element and issue a warning.
v <- 1:6
if(v %% 2) {
print("odd")
} else {
print("even")
}
[1] "odd"
Warning message:
In if (v%%2) { :
the condition has length > 1 and only the first element will be used
The solution to this is the ifelse()
function. The ifelse()
function checks the condition for every element of a vector and selects elements from the specified vector depending upon the result.
Here’s the syntax for the ifelse()
function.
Syntax
Examples
v <- c(1,2,3,4,5,6)
ifelse(v %% 2 == 0, "even", "odd")
[1] "odd" "even" "odd" "even" "odd" "even"
You can even use this function to choose values from two vectors.
v1 <- c(1,2,3,4,5,6)
v2 <- c("a","b","c","d","e","f")
ifelse(c(TRUE,FALSE,TRUE,FALSE,TRUE,FALSE), v1, v2)
[1] "1" "b" "3" "d" "5" "f"