-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexercise10.Rmd
155 lines (115 loc) · 3.16 KB
/
exercise10.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
## Exercise 10: If statement
Create the script "exercise10.R" and save it to the "Rcourse/Module2" directory: you will save all the commands of exercise 10 in that script.
<br>Remember you can comment the code using #.
<details>
<summary>
correction
</summary>
```{r, eval=F}
getwd()
setwd("~/Rcourse/Module2")
```
</details>
**1- Create vector vec2 as:**
```{r}
vec2 <- c("kiwi", "apple", "pear", "grape")
```
* Use an if statement and the %in% function to check whether "apple" is present in vec2 (in such case print "there is an apple!")
<details>
<summary>
correction
</summary>
```{r}
if("apple" %in% vec2){
print("there is an apple there")
}
```
</details>
* Use an if statement and the %in% function to check whether "grapefruit" is present in vec2: if "grapefruit" is not found, test for a second condition (using **else if**) that checks if "pear" is found.
<details>
<summary>
correction
</summary>
```{r}
if("grapefruit" %in% vec2){
print("there is a grapefruit there")
}else if("pear" %in% vec2){
print("there is no grapefruit but there is a pear")
}
```
</details>
* Add an **else** section in case neither grapefruit nor pear is found in vec2.<br>
Test your **if** statement also on vec3:
```{r}
vec3 <- c("cherry", "strawberry", "blueberry", "peach")
```
<details>
<summary>
correction
</summary>
```{r}
if("grapefruit" %in% vec2){
print("there is a grapefruit there")
}else if("pear" %in% vec2){
print("there is no grapefruit but there is a pear")
}else{
print("no grapefruit and no pear")
}
```
</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)
```
Loop over rows with **for** of mat4 and print row number and entire row **if** you find an NA.
<details>
<summary>
correction
</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")
```
Loop over each element of "vec4":
* If the element is a **human gene (all upper-case characters)**, print a vector of two elements: the name of the gene and "human gene".<br>
* If the element is a **mouse gene (only the first character is 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*
<br>
<details>
<summary>
correction
</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
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"))
}
}
```
</details>