19.1 Exercise 12: For loop
Create the script “exercise10.R” and save it to the “Rcourse/ExtraModule” directory: you will save all the commands of exercise 10 in that script.
Remember you can comment the code using #.
Answer
getwd()
setwd("~/Rcourse/ExtraModule")
1- Write a for loop that iterates over the sequence of numbers 2 to 10 (both included), and prints the square root of each number (function sqrt()).
Answer
for(i in 2:10){
print(sqrt(i))
}
2- Write a for loop that iterates over the sequence of numbers 5 to 15 (both included), and prints a new vector of 2 elements containing the number and its square root
Answer
for(i in 5:15){
<- c(i, sqrt(i))
veci print(veci)
}
3- Create the following matrix
<- matrix(rnorm(40), nrow=20) mat1
- Write a for loop that iterates over each row of mat1 and prints the median value of each row.
Answer
for(j in 1:nrow(mat1)){
# extract the row
<- mat1[j,]
rowj # compute the median
<- median(rowj)
medrowj # print median value in row
print(medrowj)
}
# with less lines of code:
for(j in 1:nrow(mat1)){
# print median value in row
print(median(mat1[j,]))
}
2- If statement in for loop
Create the following matrix:
<- matrix(c(2, 34, 1, NA, 89, 7, 12, NA, 0, 38),
mat4 nrow=5)
Write a for loop that iterates over each row of mat4: if an NA is found, print the row number and the entire row where the NA is found.
Answer
for(k in 1:nrow(mat4)){
# extract row
<- mat4[k,]
rowk if(any(is.na(rowk))){
print(k)
print(rowk)
} }
3- For loop, if statement and regular expression
Create vector vec4 as:
<- c("Oct4", "DEPP", "RSU1", "Hk2", "ZNF37A", "C1QL1", "Shh", "Cdkn2a") vec4
vec4 contains Human and Mouse gene symbols.
Loop over each element of vec4:
- If the element is a human gene (both characters and numbers; all characters are written in upper-case), print a vector of two elements: the name of the gene and “human gene.”
- If the element is a mouse gene (both characters and numbers; only the first character is written in upper-case), print a vector of two elements: the name of the gene and “mouse gene.”
Tip 1: Use grep and a regular expression in the if statement !
Tip 2: When grep does not find a match, it returns an element of length 0 !
Tip 3: You can also use grepl: check the help page
How do you get started?
- First, work on the regular expressions: how do you retrieve Human genes only? How do you retrieve Mouse genes only?
- Then, integrate the regular expressions in the if statement.
- Finally, integrate all this in a for loop!
Answer
for(gene in vec4){
if(length(grep(pattern="^[A-Z0-9]+$", x=gene)) != 0){
print(c(gene, "human gene"))
else if(length(grep(pattern="^[A-Z]{1}[a-z0-9]+$", x=gene)) != 0){
}print(c(gene, "mouse gene"))
}
}
# With grepl
# grepl returns a logical vector (match or not for each element of x), not the matching elements themselves.
for(gene in vec4){
if(grepl(pattern="^[A-Z0-9]+$", x=gene)){
print(c(gene, "human gene"))
else if(grepl(pattern="^[A-Z]{1}[a-z0-9]+$", x=gene)){
}print(c(gene, "mouse gene"))
}
}
# easier way! whenever a lower case character is present: it is from mouse
for(gene in vec4){
if(length(grep(pattern="[[:lower:]]", x=gene)) != 0){
print(c(gene, "mouse gene"))
else{
}print(c(gene, "human gene"))
} }