R Conditional Statements
A decision structure requires the programmer to specify one or more conditions to be evaluated or tested, along with a statement (required) to execute if the condition is true and an optional statement to execute if the condition is false.
The following is the general form of a typical decision structure in most programming languages:

R provides the following types of conditional statements:
- if statement
- if...else statement
- switch statement
if statement
An if statement consists of a Boolean expression followed by one or more statements.
The syntax format is as follows:
if(boolean_expression) {
// statement to be executed if the boolean expression is true
}
If the Boolean expression boolean_expression is true, the code here is executed; if it is false, it is not executed.
Example
x
<-
50L
if
(
is.integer
(
x
)
)
{
(
"X is an integer"
)
}
The output of the code above is:
[1] "X 是一个整数"
if...else statement
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
The syntax format is as follows:
if(boolean_expression) {
// statement to execute if the boolean expression is true
} else {
// statement to execute if the boolean expression is false
}
If the boolean expression boolean_expression is true, the code inside the if block is executed. If the boolean expression is false, the code inside the else block is executed.
Example
x
<-
c
(
"google"
,
"runoob"
,
"taobao"
)
if
(
"runoob"
%
in
%
x
)
{
(
"contains runoob"
)
}
else
{
(
"does not contain runoob"
)
}
The output of the code above is:
[1] "包含 runoob"
If there are multiple conditions to check, you can use if...else if...else:
if(boolean_expression 1) {
// statement to execute if boolean_expression 1 is true
} else if( boolean_expression 2) {
// statement to execute if boolean_expression 2 is true
} else if( boolean_expression 3) {
// statement to execute if boolean_expression 3 is true
} else {
// Execute when all the above boolean expressions are false
}
Example
x
<-
c
(
"google"
,
"runoob"
,
"taobao"
)
if
(
"weibo"
%
in
%
x
)
{
(
"the first if contains weibo"
)
}
else
if
(
"runoob"
%
in
%
x
)
{
(
"the second if contains runoob"
)
}
else
{
(
"not found"
)
}
The output of the code above is:
[1] "第二个 if 包含 runoob"
switch statement
A switch statement allows testing a variable against multiple values. Each value is called a case.
The syntax format is as follows:
switch(expression, case1, case2, case3....)
switch statements must follow these rules:
- switch The expression is a constant expression, which can be an integer or a string. If it is an integer, the corresponding case position value is returned; if the integer is not within the range of positions, NULL is returned.
- If multiple values match, the first one is returned.
- expressionIf it is a string, the value corresponding to the variable name in the case is returned; if there is no match, no value is returned.
- switch has no default parameter available.
The following example returns the third value:
Example
x
<-
switch
(
3
,
"google"
,
"runoob"
,
"taobao"
,
"weibo"
)
(
x
)
The output of the code above is:
[1] "taobao"
If it is a string, the value of the corresponding string variable is returned:
Example
you.
like
<-
"runoob"
switch
(
you.
like
=
"www.google.com"
, runoob
=
"www.runoob.com"
, taobao
=
"www.taobao.com"
)
The output of the code above is:
[1] "www.runoob.com"
If the integer is out of range, NULL is returned
Example
x
<-
switch
(
4
,
"google"
,
"runoob"
,
"taobao"
)
x
NULL
x
<-
switch
(
4
,
"google"
,
"runoob"
,
"taobao"
)
x
NULL