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.

x <- 1:10
x
##  [1]  1  2  3  4  5  6  7  8
##  [9]  9 10
x == 1
##  [1]  TRUE FALSE FALSE FALSE
##  [5] FALSE FALSE FALSE FALSE
##  [9] FALSE FALSE
x != 1
##  [1] FALSE  TRUE  TRUE  TRUE
##  [5]  TRUE  TRUE  TRUE  TRUE
##  [9]  TRUE  TRUE
x >= 8
##  [1] FALSE FALSE FALSE FALSE
##  [5] FALSE FALSE FALSE  TRUE
##  [9]  TRUE  TRUE
x <= 8
##  [1]  TRUE  TRUE  TRUE  TRUE
##  [5]  TRUE  TRUE  TRUE  TRUE
##  [9] FALSE FALSE
x > 1 & x < 4
##  [1] FALSE  TRUE  TRUE FALSE
##  [5] FALSE FALSE FALSE FALSE
##  [9] FALSE FALSE
x == 2 | x > 8
##  [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.

a <- c(-5, -3, -1, 0, 2, 4, 6)
b <- c(-5, -3, -1, 0, 1, 3, 5)

a == b
## [1]  TRUE  TRUE  TRUE  TRUE
## [5] FALSE FALSE FALSE
a > b
## [1] FALSE FALSE FALSE FALSE
## [5]  TRUE  TRUE  TRUE
  • Note that NAs, or the missing values, are contagious in logical operators. That is, logical operators will return NA if one of the elements being compared is NA. For example:
d <- c(1, NA, 3)
e <- c(1, 2, 3)
d == e
## [1] TRUE   NA TRUE
e > NA
## [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:

which(a > 3)
## [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.

a[a > 3]
## [1] 4 6
# or
a[which(a > 3)]
## [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.

if (length(a) == 7) {
  print('a has length 7.')
} else {                                   
  print('a does not have length 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

  1. 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.

  2. In the last chapter, we imported the data frame exercise2.1. Create objects named exp and len corresponding to the first and the second variable respectively. Find all elements of len, such that its corresponding exp is 'smile'.

  • You can import the data set by running the below code chunk:
example2.1 <- read.table("https://raw.githubusercontent.com/sunbeomk/PSYC490/main/example2.1.txt", fill = NA, row.names = 1, header = TRUE)