A Boolean is something that is either true or false. In R, the values TRUE and FALSE must be typed with capital letters:
y <- TRUE
n <- FALSE
You can also create a Boolean by testing equality. To do this, use two equal signs: ==
# Is 1 equal to 1?
1 == 1
## [1] TRUE
Remember that a SINGLE equals sign is used to assign a value to a variable while a DOUBLE equals sign is used to test for equality:
# Assign a value of 1 to a
a <- 1
# Assign a value of 1 to b
b <- 1
# Does a equal b?
a == b
## [1] TRUE
This means that you can test equality using ==
, generate a Boolean, then assign this Boolean to a variable using =
:
# The statement "(1 == 1)" evaluates to "TRUE" and then we assign that result
# to the variable "bool"
bool <- (1 == 1)
# Is 1 equal to 1?
print(bool)
## [1] TRUE
In addition to equality (==
) you can test if two things are NOT equal using !=
. In R, the exclamation mark means ‘not’:
# Is 1 not equal to 1?
bool <- (1 != 1)
print(bool)
## [1] FALSE
# Is 1 not equal to 2?
bool <- (1 != 2)
print(bool)
## [1] TRUE
You can use >
and <
to test if something is greater than or less than something else. Use >=
to test if something is greater than or equal to something else and <=
to test ‘less than or equal to’:
# Is 1 less than 2?
print(1 < 2)
## [1] TRUE
# Is attendance below or at capacity?
attendance <- 95
capacity <- 100
print(attendance <= capacity)
## [1] TRUE
You can combine conditionals using ‘and’ and ‘or’ statements.
An ‘and’ statement is created by using an ampersand (&
) between two conditionals. In order for an ‘and’ statement to evaluate to TRUE
both conditionals must be true:
# Is attendance below or at capacity AND
# has everyone bought a ticket?
attendance <- 95
capacity <- 100
tickets_sold <- 93
print((attendance <= capacity) & (tickets_sold == attendance))
## [1] FALSE
Notice the use of brackets in the last line. Each conditional is enclosed in round brackets to aid readability.
An ‘or’ statement is created by using a pipe (|
) between two conditionals. In order for an ‘or’ statement to evaluate to TRUE
either conditional can be true:
# Is attendance over capacity OR
# has someone not bought a ticket?
attendance <- 105
capacity <- 100
tickets_sold <- 105
print((attendance > capacity) | (tickets_sold != attendance))
## [1] TRUE
Notice that the same question is being asked as in the ‘and’ statement example, but this time in a different way.
Strings are handled similarly to numbers:
# Do these two people have the same name?
person1 <- "WINSTON CHURCHILL"
person2 <- "Winston Churchill"
print(tolower(person1) == tolower(person2))
## [1] TRUE
Notice the function tolower()
that was used above to convert both strings to lowercase before making the comparison. Strings are case sensitive, so if we hadn’t used that function the result would have been FALSE
.
You can also test if a string contains a certain sub-string or character using grepl()
:
st <- "Hello World"
print(grepl("Hello", st))
## [1] TRUE
‘grepl’ stands for ‘global regular expression print - logical’, in case you were wondering.
An ‘if’ statement is a chunk of code that will execute if a condition is true (and which will not execute if the condition is false). In order to write an if statement you need to:
if (1 == 1) {
print("This will print to console because 1 is equal to 1")
}
## [1] "This will print to console because 1 is equal to 1"
An ‘else’ statement is a chunk of code that will execute if the ‘if’ statement does not execute:
height <- 170
if (height >= 180) {
print("This person is tall")
} else {
print("This person is not tall")
}
## [1] "This person is not tall"
An ‘else if’ statement is like a second ‘if’ statement. It is a chunk of code that will execute if the first ‘if’ statement does not evaluate to true but the second one does:
height <- 170
if (height >= 180) {
print("This person is tall")
} else if (height < 180 & height >= 160) {
print("This person is medium height")
} else {
print("This person is short")
}
## [1] "This person is medium height"
Besides the grepl()
function mentioned above, another way to check if a string contains a sub-string is to use the gregexpr()
(global regular expression) function. This returns the index of a sub-string or -1
if it cannot find it:
st <- "Hello World"
# Search for the letter "x" in the string "Hello World"
idx_x <- unlist(gregexpr(pattern = "x", st))
# If the letter "x" is found, print a message that says that it has been found.
# If the letter "x" is not found, print the opposite message.
if (idx_x == -1) {
print("The string does NOT contain the letter x")
} else {
print("The string DOES contain the letter x")
}
## [1] "The string does NOT contain the letter x"
As expected, the letter “x” was not found in “Hello World”.
Imagine you are running some code using a dataset that someone gave you and which contains some errors. For example, maybe they entered someone’s birth year as 1890 instead of 1980 and so you calculate their age as being 130 instead of 40. You can use if statements together with the stop()
function to sanitise your data and check that these types of errors don’t occur:
age <- current_year - birth_year
if (age <= 110) {
print("This person is not older than 110 years")
} else {
stop("This person is older than 110 years. Are you sure this is correct?")
}
The above code will cause the script to stop and produce the message:
This person is older than 110 years. Are you sure their age is correct?
which is a lot better than having it run to completion and generating incorrect results.