Week 3 Control Flow & Functions
3.1 Logical Operators
We can create logical vectors indicating whether each element of a vector satisfies some conditions.
Logical operator | Example |
---|---|
equal to | x == 1 |
not equal to | x != 1 |
greater than | x > 7 |
less than | x < 7 |
greater than or equal to | x >= 8 |
less than or equal to | x <= 8 |
and | x > 1 & x < 4 |
or | x > 8 \(|\) x == 2 |
Let’s create an atomic vector and see some examples of logical operators. Logical operators will specify all the elements that satisfy some conditions. For instance, x == 1
will return a logical vector indicating whether each element of x
is equal to 1
.
## [1] 1 2 3 4 5 6 7 8
## [9] 9 10
## [1] TRUE FALSE FALSE FALSE
## [5] FALSE FALSE FALSE FALSE
## [9] FALSE FALSE
## [1] FALSE TRUE TRUE TRUE
## [5] TRUE TRUE TRUE TRUE
## [9] TRUE TRUE
## [1] FALSE FALSE FALSE FALSE
## [5] FALSE FALSE FALSE TRUE
## [9] TRUE TRUE
## [1] TRUE TRUE TRUE TRUE
## [5] TRUE TRUE TRUE TRUE
## [9] FALSE FALSE
## [1] FALSE TRUE TRUE FALSE
## [5] FALSE FALSE FALSE FALSE
## [9] FALSE FALSE
## [1] FALSE TRUE FALSE FALSE
## [5] FALSE FALSE FALSE FALSE
## [9] TRUE TRUE
See what happens if we use logical operators between two vectors of the same length.
## [1] TRUE TRUE TRUE TRUE
## [5] FALSE FALSE FALSE
## [1] FALSE FALSE FALSE FALSE
## [5] TRUE TRUE TRUE
- Note that
NA
s, or the missing values, are contagious in logical operators. That is, logical operators will returnNA
if one of the elements being compared isNA
. For example:
## [1] TRUE NA TRUE
## [1] NA NA NA
which()
function returns the indices for the elements of a vector satisfying certain conditions. For example, to find which elements of a
are greater than 3 by:
## [1] 6 7
We can refer to the elements of a vector satisfying some conditions by using logical operators with brackets []
. The following code will return the values of the vector a
that are greater than 3.
## [1] 4 6
## [1] 4 6
3.2 if
statement
An if
statement allows us to conditionally execute code. Here is an example of how to use an if
statement. The following code will print "a has length 7"
if the length of a
is 7.
## [1] "a has length 7."
The statement to be tested goes into ()
, and the consequence for “statement is true” goes into the first braces {}
. The consequence for “statement is false” goes into the second {}
after else
.
In an if
statement, we should not use &
or |
because these are vectorised operations that apply to multiple values. Instead, you can use &&
or ||
.
if (length(x) == 10 && x[1] > 0) {
print('x has length 10, and the first element of x is greater than zero.')
}
## [1] "x has length 10, and the first element of x is greater than zero."
3.3 Your turn
Write an
if
statement that prints “This statement is true” if x has length greater than 5, or the first element of x is greater than 5.In the last chapter, we imported the data frame
exercise2.1
. Create objects namedexp
andlen
corresponding to the first and the second variable respectively. Find all elements oflen
, such that its correspondingexp
is'smile'
.
- You can import the data set by running the below code chunk: