Page 204 - Data Science class 11
P. 204
6.2.4 Booleans (Logical Values)
In programming, you often need to know if an expression is true or false. You can evaluate any expression in R and
get one of two answers: TRUE or FALSE.
When you compare two values, the expression is evaluated and R returns the logical answer.
Example
Enter the following code snippet in the script panel:
10 > 9 # TRUE because 10 is greater than 9
10 == 9 # FALSE because 10 is not equal to 9
10 < 9 # FALSE because 10 is greater than 9
Run to see the following in the console panel:
You can also run a condition in an if statement, which you will learn much more about in the if..else.
• if-else statement—This is a conditional element selection statement that executes a block of code if a specified
condition is true. If the condition is false, another block of code can be executed.
Example
if (age >=18)
{
print(“You are eligible to vote”)
}
else
{
print(“You are not eligible to vote”)
}
Example
Enter the following code snippet in the script panel:
a <- 200
b <- 33
if (b > a) {
print ("b is greater than a")
} else {
print("b is not greater than a")
}
202 Touchpad Data Science-XI

