When developing computer code, we often need to ask the system questions about the contents of a variable or multiple variables (e.g., has the user submitted an existing file, provided a valid response, or entered a password?). This is the purpose of conditional structures.
A conditional structure allows testing whether a condition is true or false. These conditional structures generally evaluate a test that returns a logical variable (true/false):
- a > 4
- b < 4
- 5 < b & b < 10
- a == ‘hamster’
- a %in% c(‘hamster’, ‘dog’, ‘cat’)
- …
In the classic if/else structure, a set of instructions is executed if the condition is true, and a different set of instructions is executed if the condition is false.
b <- 3
if(b < 4) {
cat("It's true.\n")
cat("I confirm.")
} else {
cat("It's false.\n")
cat("I confirm that it's false!")
}
You may also encounter if without else.
if(b < 4) {
cat("It's true.\n")
cat("I confirm.")
}
Or even if/else if/else.
x <- 3
if(x == 1) {
cat("x is 1.\n")
} else if(x == 2) {
cat("x is 2.\n")
} else {
cat("x is neither 1 nor 2.\n")
}
The ifelse() Function
In addition to the classic constructions we just reviewed, which are
found in many programming languages, R offers a vectorized version. The
ifelse()
function allows iteration over a vector. Its
syntax is as follows:
ifelse(boolean_vector, value_returned_if_position_is_true,
value_returned_if_position_is_false)
A more concret example:
set.seed(123)
x <- sample(c(1,2), size=10, replace=TRUE)
print(x)
x <- ifelse(x == 1, 'chat', 'hamster')
print(x)
- You could achieve the same result as the previous one by converting x into a factor, modifying the levels, and then converting x into a character string. What would the code look like?
set.seed(123)
x <- sample(c(1,2), size=10, replace=TRUE)
x <- factor(x)
levels(x) <- c('cat', 'hamster')
x <- as.character(x)
# or in a single line...
x <- as.character(factor(x, levels = c('cat', 'hamster')))
Exercises
- Download the following tab-delimited file containing information on 10,000 human transcripts. This information was obtained from the ensembl database.
- Display the column names.
- Using the
ifelse()
function, create a new column named mono_exonic with the value ‘mono’ if the transcript contains one exon (columnnb_exons
), or ‘multi’ otherwise. - Use
table()
to count the occurrences of mono and multi in the mono_exonic column. Store the result incount_mono_exonic
. - Represent
count_mono_exonic
with thebarplot()
function.
## The URL pointing to the dataset
url <- "https://zenodo.org/record/8171242/files/Homo_sapiens.GRCh38.110.chr_10k.tsv"
tx_info <- read.table(file=url, header=TRUE, sep="\t", row.name=11)
## The URL pointing to the dataset
url <- "https://zenodo.org/record/8171242/files/Homo_sapiens.GRCh38.110.chr_10k.tsv"
tx_info <- read.table(file=url, header=TRUE, sep="\t", row.name=11)
tx_info$mono_exonic <- ifelse(tx_info$nb_exons == 1, 'mono', 'multi')
count_mono_exonic <- table(tx_info$mono_exonic)
barplot(count_mono_exonic)
End of the section
Thank you for following this tutorial.