Boolean expressions are logical statements that are either true or false. For our purposes, we will often use Boolean expressions to compare quantities. For instance, the Boolean expression 1 < 2 is true, whereas the Boolean expression 1 > 2 is false.
When you type a Boolean expression in R, R will output TRUE
if the expression is true and FALSE
if the expression is false. In R, typing a < b
outputs whether a
is less than b
, typing a > b
outputs whether a
is greater than b
, and typing a == b
outputs whether a
is equal to b
. Note that we use a double equal sign ==
to check whether two values are equal. Typing a = b
would set the value of a
equal to the value of b
.
Here are some examples of Boolean expressions in action:
1 < 2
## [1] TRUE
1 > 2
## [1] FALSE
1 == 2
## [1] FALSE
value1
and set value1
equal to 1. Then write a Boolean expression that outputs TRUE
if value1
equals 1.value1 = 1
value1 ==1
## [1] TRUE
In R, the expressions <=
(less than or equal to), >=
(greater than or equal to), and !=
(not equal to) work similarly to <
, >
, and ==
.
value2
that is equal to value1
plus 10. Write three Boolean expressions that checkvalue1
and value2
is less than or equal to 12.value1
and value2
is greater than or equal to 10.value2
minus value1
is not equal to 0. (Use !=
.)value2 = value1+10
value1+value2<=12
## [1] TRUE
value1*value2>=10
## [1] TRUE
value2-value1 !=0
## [1] TRUE
As with many expressions in R, the Boolean expressions discussed above are all vectorized. For an example, consider the Boolean expression ==
. If vector1
and vector2
are two vectors of the same length, then vector1 == vector2
outputs a vector of that same length. Element i
of the vector created by vector1 == vector2
equals TRUE
if vector1[i] == vector2[i]
and equals FALSE
otherwise. A vector in which every element is TRUE
or FALSE
is called a Boolean vector. Conveniently, if you perform the sum
function on a Boolean vector, sum
will return the number of TRUE
elements in the Boolean vector. The code below demonstrates these properties.
vector1 <- c(0, 1, 2)
vector2 <- c(0, 2, 2)
vector1 == vector2
## [1] TRUE FALSE TRUE
sum(vector1 == vector2)
## [1] 2
vector1
and vector2
each contain 10 values. Using the fact that Boolean expressions are vectorized, write code that outputsi
of this vector equals TRUE
if vector1[i]
is less than vector2[i]
and equals FALSE
otherwise.i
of vector1
is less than element i
of vector2
, using the sum
function.vector1 <- c(1, 2, 4, 5, 3, 7, 8, 7, 1, 2)
vector2 <- c(1, 3, 4, 4, 5, 10, 6, 8, 9, 1)
vector1 <= vector2
## [1] TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE FALSE
sum(vector1 <= vector2)
## [1] 7
A conditional statement is a declaration that if a certain condition holds, then a certain event must take place. For instance, “IF the temperature is above freezing, THEN I will take advantage of the nice weather and go for a walk.”
There is a natural connection between Boolean expressions and conditional statements. The condition that proceeds the “if” in a conditional statement is a Boolean expression. So conditional statements express that if a Boolean expression is true, then a certain action must be performed. In R, we use conditional statements if we want to run some code only when some Boolean expression is true. The general R syntax for a conditional statement is
if(Boolean expression){
# code for action that is executed if Boolean expression is true.
}
For instance, we might have a variable walk
that is currently equal to 0. We want to set walk
equal to 1 if the temperature is above freezing, to indicate that you go for a walk.
walk = 0
if(temperature > 32){
walk = 1
}
In this code, if temperature
is greater than 32, then walk
is set equal to 1. Otherwise, walk
is unchanged, meaning that walk
will equal 0.
balance
that he will update whenever the fund’s balance changes. If balance
equals 0, Jerry adds $100 to balance
. Jerry types the following:balance = 10
if(balance == 0){
balance = 100
}
If you evaluate this code (remove eval = F
), it will produce an error message. Fix Jerry’s code.
balance = 10
if(balance == 0){
balance = 100
}
In many situations, we want to perform a certain action if a condition is met, and we want to perform a different action if the condition is not met. In R, the general syntax for this situation is
if(Boolean expression){
# code for action that occurs if Boolean expression is true
} else {
# code for action that occurs if Boolean expression is false
}
As an example, suppose you check the temperature and it is below freezing. When the temperature is below freezing, you read your economics textbook instead of going for a walk. You could code this in the following manner:
walk = 0
read = 0
if(temperature > 32){
walk = 1
} else {
read = 1
}
balance
according to those guidelines.balance = 10
if(balance ==0){
balance = 100
} else {
balance = balance/2
}
balance
## [1] 5