0% found this document useful (0 votes)
11 views4 pages

Understanding Conditional Statements in R

The document explains conditional statements in programming, specifically in R, which are used for decision-making based on evaluated conditions. It covers three main types: if statements, if-else statements, and switch statements, providing examples for each. These structures allow programmers to execute specific code based on whether certain criteria are met.

Uploaded by

Squall Lionheart
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Understanding Conditional Statements in R

The document explains conditional statements in programming, specifically in R, which are used for decision-making based on evaluated conditions. It covers three main types: if statements, if-else statements, and switch statements, providing examples for each. These structures allow programmers to execute specific code based on whether certain criteria are met.

Uploaded by

Squall Lionheart
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2.

7 CONDITIONAL STATEMENTS

Dspecify
ecision making structures require the programmer to
one or more conditions to be evaluated or tested by
the program, along with a statement or statements to be
executed if the condition is determined to be true, and
optionally, other statements to be executed if the condition is
determined to be false.

Following is the general form of a typical decision making

structure found in most of the programming languages −

In the world of R, data analysis often involves making


decisions based on speci c conditions. This is where
conditional statements come in –powerful tools that control
the ow of your code based on whether certain criteria are
met. Here, we'll delve into three essential conditional
statements: if, else if, and switch.

Page 67 of 77
fl
fi
Statement & Description
if statement
An if statement consists of a Boolean expression followed
by one or more statements.
if...else statement
An if statement can be followed by an optional else
statement, which executes when the Boolean expression is
false.
switch statement
A switch statement allows a variable to be tested for
equality against a list of values.

IF Statement:
Imagine you're analyzing customer data and want to o er
a discount if their purchase amount exceeds a certain
threshold. The if statement allows you to execute speci c
code only when a condition is TRUE. Here's the basic
structure:

if (condition) {
# Code to execute if the condition is TRUE
}

Here is an example code using if statement:


I n t h i s ex a m p l e , t h e i f s t a t e m e n t c h e c k s i f
purchase_amount is greater than discount_threshold. If
TRUE, the code within the curly braces calculates the
discount and prints a message.

Page 68 of 77
ff
fi
purchase_amount <- 150
discount_threshold <- 100

if (purchase_amount > discount_threshold) {


discount <- purchase_amount * 0.1 # Calculate 10% discount
print(paste("Congratulations! You qualify for a", discount, “discount."))
}

IF-ELSE Statement:
Life is rarely a simple yes or no. The else if statement
extends the if functionality by allowing you to check
additional conditions if the initial one is FALSE. Here's the
syntax:

if (condition1) {
# Code to execute if condition1 is TRUE
} else if (condition2) {
# Code to execute if condition1 is FALSE and condition2 is TRUE
}

This example uses nested if and else if statements to


determine the grade based on the exam score and the
grading scale de ned as a named vector.

exam_score <- 85
grading_scale <- c("A" = 90:100, "B" = 80:89, "C" = 70:79)

if (exam_score >= grading_scale$A[1]) {


grade <- "A"
} else if (exam_score >= grading_scale$B[1]) {
grade <- "B"
} else {

Page 69 of 77
fi
grade <- "C" # Implicit else block if all previous conditions are FALSE
}
print(paste("Your exam score:", exam_score, "results in a grade of:", grade))

Switch Statement:
The else if statement extends the if functionality by
allowing you to check additional conditions if the initial one is
FALSE. Here's the syntax:

switch (expression, value1 = code1,


value2 = code2,
...,
default = code_default)

This example uses the switch statement to check the


value of the fruit variable against available options. If a match
is found, the corresponding code block is executed, printing a
message. The default clause handles cases where the fruit is
not listed.

fruit <- "apple"


available_fruits <- c("apple", "banana", “orange")

switch (fruit,
apple = print("We have delicious apples!”),
banana = print("Fresh bananas are in stock!”),
orange = print("Get your vitamin C with our oranges!”),
default = print("Sorry, we don't have that fruit in stock.")
)

Page 70 of 77

You might also like