1 == 1 # is 1 equal to 1?
[1] TRUE
1 == 2 # is 1 equal to 2?
[1] FALSE
c(1, 2, 3) == c(2, 1, 3) # compares each element with each element
[1] FALSE FALSE TRUE
1 == c(2, 1, 3)
[1] FALSE TRUE FALSE
Matt Crump
February 21, 2023
Logic statements are used to compare two things, or two sets of things. The output of comparison is a TRUE or FALSE statment. If many things are being compared at once, the output could be many TRUE or FALSE statements for each comparison
[1] FALSE
[1] TRUE
[1] FALSE
[1] FALSE
[1] FALSE TRUE FALSE
[1] TRUE FALSE FALSE
[1] TRUE FALSE FALSE
Is something greater than or equal to something else
[1] TRUE
[1] TRUE
[1] FALSE
[1] FALSE
[1] FALSE TRUE TRUE
[1] TRUE FALSE TRUE
[1] TRUE TRUE FALSE
The ampersand &
is used for AND, which allows use to evaluate whether two or more properties are all TRUE.
The |
is used for OR, which allows use to evaluate at least one of the properties is TRUE.
When R returns values as TRUE or FALSE, it return a logical variable. It also treats TRUE as a 1, and FALSE as a 0. In the example below we see it is possible sum up a logical variable with multiple TRUE and FALSE entries.
A roller-coaster operator checks if people are taller than a line to see if they can ride the coaster. This is an IF ELSE control structure. IF the person is taller than the line, then they can go on the ride; ELSE (otherwise) the person can not go on the ride.
In other words, IF the situation is X, then do something; ELSE (if the situation is not X), then do something different.
IF and ELSE statements let us specify the conditions when specific actions are taken. Generally, IF and ELSE statements are used inside loops (for, or while, or repeat loops), because at each step or iteration of the loop, we want to check something, and then do something.
Consider this:
a <- 1 # define a to be a 1
if (a == 1) {
print(a) # this is what happens if a==1
} else {
print("A is not 1") # this is what happens if a is not 1
}
[1] 1
a <- 2 # define a to be a 1
if (a == 1) {
print(a) # this is what happens if a==1
} else {
print(rbinom(10,1,.5)) # this is what happens if a is not 1
1+1
print("hello world")
rbinom(10,1,.5)
}
[1] 0 0 1 1 0 1 0 1 0 0
[1] "hello world"
[1] 1 0 1 1 1 0 1 0 1 0
Normally we find IF and ELSE in a loop like this:
a <- c(1, 0, 1, 0, 0, 0, 1) # make a variable contain 1s and 0s
# write a loop to check each element in the variable
# and do different things depending on the element
for (i in a) {
if (i == 1) {
print("I'm a 1") # what to do when i is 1
} else {
print("I'm not a 1") # what to do when i is not 1
}
}
[1] "I'm a 1"
[1] "I'm not a 1"
[1] "I'm a 1"
[1] "I'm not a 1"
[1] "I'm not a 1"
[1] "I'm not a 1"
[1] "I'm a 1"
We can have multiple conditions in our if statements. See the next section on loops for more info about using loops.
a <- c(1, 2, 3, 1, 2, 0, 1) # make a variable contain 1s and 0s
# write a loop to check each element in the variable
# and do different things depending on the element
for (i in a) {
if (i == 1) {
print("I'm a 1") # what to do when i is 1
} else if (i == 2) {
print("I'm a 2") # what to do when i is 2
} else if (i == 3) {
print("I'm a 3") # what to do when i is 3
} else {
print("I'm not any of the above") #what to do when none are true
}
}
[1] "I'm a 1"
[1] "I'm a 2"
[1] "I'm a 3"
[1] "I'm a 1"
[1] "I'm a 2"
[1] "I'm not any of the above"
[1] "I'm a 1"
Check R help for on Control Flow ?Control
.
for(){}
for(loop control){do something each iteration}
Loop control is defined in between the parentheses. The name of the iterator is placed on the left of in
(can be assigned any name you want, does not need to be declared in advance). During the execution of the loop, the iterator takes on the values inside the vector which is placed on the right side of in
. Specifically, the following is happening.
Loop steps: 1. iterator <- vector[1] 2. iterator <- vector[2] 3. iterator <- vector[3] 4. etc.
The loop will automatically stop once it reaches the last item in the vector. The loop can be stopped before that using the break
command.
# Make a loop do something 5 times
# i is the iterator
# 1:5 creates a vector with 5 numbers in it, 1, 2, 3, 4, 5
# the loop will run 5 times, because there are five things to assign to i
for(i in 1:5) {
print("hello")
}
[1] "hello"
[1] "hello"
[1] "hello"
[1] "hello"
[1] "hello"
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
# Reminder that i becomes the next value in the vector
# your vector can have any order
my_sequence <- c(1, 5, 2, 3, 4)
for (i in my_sequence) {
print(i)
}
[1] 1
[1] 5
[1] 2
[1] 3
[1] 4
# index vector does not need to be numbers
my_things <- c("A", "B", "C", "D")
for (i in my_things) {
print(i)
}
[1] "A"
[1] "B"
[1] "C"
[1] "D"
break
stops a loop. Used with logical statements to define the conditions necessary to cause the break.
While loops run until a logical condition is met. Here there is no iterator, just a logic statement that needs to be met.
This one prints i while i is less than 6. As soon as i becomes “not less than 6”, then the loop stops. Critically, inside the loop, the value of i increases each iteration.
Similar to while, but let’s do things until a condition is met.
Braces are not needed on one line
Using the value of the iterator to assign in values systematically to another variable.
# put 1 into the first five positions of x
x <- c() # create empty vector
for (i in 1:5) {
x[i] <- 1 # assign 1 to the ith slot in x
}
x
[1] 1 1 1 1 1
[1] 1 2 3 4 5
Make your own counter, when you need one
a <- c(1, 4, 3, 5, 7, 6, 8, 2)
odd <- c()
counter <- 0
for (i in a) {
# i will the values of a in each position
counter <- counter + 1
if (i %% 2 != 0) {
odd[counter] <- "odd"
} else {
odd[counter] <- "even"
}
}
odd
[1] "odd" "even" "odd" "odd" "odd" "even" "even" "even"
# An alternative strategy
a <- c(1, 4, 3, 5, 7, 6, 8, 2)
odd <- c()
# 1:length(a) creates a sequence from 1 to length
for (i in 1:length(a)) {
if (a[i] %% 2 != 0) {
odd[i] <- "odd"
} else {
odd[i] <- "even"
}
}
odd
[1] "odd" "even" "odd" "odd" "odd" "even" "even" "even"
Nesting loops
[1] 1 1
[1] 1 2
[1] 1 3
[1] 1 4
[1] 1 5
[1] 2 1
[1] 2 2
[1] 2 3
[1] 2 4
[1] 2 5
[1] 3 1
[1] 3 2
[1] 3 3
[1] 3 4
[1] 3 5
[1] 4 1
[1] 4 2
[1] 4 3
[1] 4 4
[1] 4 5
[1] 5 1
[1] 5 2
[1] 5 3
[1] 5 4
[1] 5 5
# example of using nested loops to fill the contents
# of a matrix
my_matrix <- matrix(0, ncol = 5, nrow = 5)
for (i in 1:5) {
for (j in 1:5) {
my_matrix[i, j] <- i * j
}
}
my_matrix
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 2 4 6 8 10
[3,] 3 6 9 12 15
[4,] 4 8 12 16 20
[5,] 5 10 15 20 25
break exits out of the immediate loop