-
Notifications
You must be signed in to change notification settings - Fork 1
/
exercise12_for.Rmd
176 lines (131 loc) · 3.82 KB
/
exercise12_for.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
## 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.
<br>Remember you can comment the code using #.
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
getwd()
setwd("~/Rcourse/ExtraModule")
```
</details>
**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()).**
<details>
<summary>
*Answer*
</summary>
```{r}
for(i in 2:10){
print(sqrt(i))
}
```
</details>
**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**
<details>
<summary>
*Answer*
</summary>
```{r}
for(i in 5:15){
veci <- c(i, sqrt(i))
print(veci)
}
```
</details>
**3- Create the following matrix**
```{r}
mat1 <- matrix(rnorm(40), nrow=20)
```
* Write a for loop that iterates over each row of **mat1** and prints the **median value of each row**.
<details>
<summary>
*Answer*
</summary>
```{r}
for(j in 1:nrow(mat1)){
# extract the row
rowj <- mat1[j,]
# compute the median
medrowj <- median(rowj)
# 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,]))
}
```
</details>
**2- If statement in for loop**
Create the following matrix:
```{r}
mat4 <- matrix(c(2, 34, 1, NA, 89, 7, 12, NA, 0, 38),
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.
<details>
<summary>
*Answer*
</summary>
```{r}
for(k in 1:nrow(mat4)){
# extract row
rowk <- mat4[k,]
if(any(is.na(rowk))){
print(k)
print(rowk)
}
}
```
</details>
**3- For loop, if statement and regular expression**
Create vector **vec4** as:
```{r}
vec4 <- c("Oct4", "DEPP", "RSU1", "Hk2", "ZNF37A", "C1QL1", "Shh", "Cdkn2a")
```
**vec4** contains Human and Mouse gene symbols.
<br>
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".<br>
* 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".<br>
> Tip 1: *Use grep and a regular expression in the if statement !*<br>
> Tip 2: *When grep does not find a match, it returns an element of **length 0** !*<br>
> 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!
<br>
<details>
<summary>
*Answer*
</summary>
```{r}
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"))
}
}
```
</details>