Easy Tutorial
❮ R Linear Regression R Vector ❯

R Conditional Statements

Conditional structures require the programmer to specify one or more conditions to be evaluated or tested by the program, along with the statements to be executed if the condition is true (required) and the statements to be executed if the condition is false (optional).

Below is the general form of a typical conditional structure in most programming languages:

R provides the following types of conditional statements:

if Statement

An if statement consists of a Boolean expression followed by one or more statements.

if(boolean_expression) {
    // statements to be executed if the Boolean expression is true
}

If the Boolean expression boolean_expression is true, the code inside the if block will be executed. If it is false, the code inside the if block will not be executed.

Example

x <- 50L
if(is.integer(x)) {
   print("X is an integer")
}

Executing the above code will output:

[1] "X is an integer"

if...else Statement

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

Syntax:

if(boolean_expression) {
    // statements to be executed if the Boolean expression is true
} else {
    // statements to be executed if the Boolean expression is false
}

If the Boolean expression boolean_expression is true, the code inside the if block will be executed. If it is false, the code inside the else block will be executed.

Example

x <- c("google", "tutorialpro", "taobao")

if("tutorialpro" %in% x) {
   print("Contains tutorialpro")
} else {
   print("Does not contain tutorialpro")
}

Executing the above code will output:

[1] "Contains tutorialpro"

For multiple conditional checks, you can use if...else if...else:

if(boolean_expression1) {
    // statements to be executed if boolean_expression1 is true
} else if(boolean_expression2) {
    // statements to be executed if boolean_expression2 is true
} else if(boolean_expression3) {
    // statements to be executed if boolean_expression3 is true
} else {
    // statements to be executed if all above Boolean expressions are false
}

Example

x <- c("google", "tutorialpro", "taobao")

if("weibo" %in% x) {
   print("First if contains weibo")
} else if ("tutorialpro" %in% x) {
   print("Second if contains tutorialpro")
} else {
   print("Not found")
}

Executing the above code will output:

[1] "Second if contains tutorialpro"

switch Statement

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case.

Syntax:

switch(expression, case1, case2, case3....)

Rules for switch statement:

Example

x <- switch(
   3,
   "google",
   "tutorialpro",
   "taobao",
   "weibo"
)
print(x)

Executing the above code will output:

[1] "taobao"

For string expressions, it returns the corresponding string value:

Example

you.like <- "tutorialpro"
switch(you.like, google = "www.google.com", tutorialpro = "www.tutorialpro.org", taobao = "www.taobao.com")

Executing the above code will output:

[1] "www.tutorialpro.org"

If the integer is out of range, it returns NULL:

Example

x <- switch(
   5,
   "google",
   "tutorialpro",
   "taobao",
   "weibo"
)
print(x)

Executing the above code will output:

NULL
> x <- switch(4, "google", "tutorialpro", "taobao")
> x
NULL
> x <- switch(4, "google", "tutorialpro", "taobao")
> x
NULL
❮ R Linear Regression R Vector ❯