-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexercise3.Rmd
204 lines (142 loc) · 3.09 KB
/
exercise3.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
## Exercise 3. Character vector manipulation
### Exercise 3a.
Create the script "exercise3.R" and save it to the "Rcourse/Module1" directory: you will save all the commands of exercise 3 in that script.
<br>Remember you can comment the code using #.
**1- Go to Rcourse/Module1
First check where you currently are with getwd();
then go to Rcourse/Module1 with setwd()**
<details>
<summary>
correction
</summary>
```{r, eval=F}
getwd()
setwd("Rcourse/Module1")
setwd("~/Rcourse/Module1")
```
</details>
**2- Create vector w as:**
```{r}
w <- rep(x=c("miRNA", "mRNA"), times=c(3, 2))
```
**3- View vector w in the console: how does function rep() work ?**
<br>Play with the **times** argument.
<details>
<summary>
correction
</summary>
```{r}
rep(x=c("miRNA", "mRNA"), times=c(3, 4))
rep(x=c("miRNA", "mRNA"), times=c(10, 2))
```
</details>
**4- What is the output of table(w) ? What does the table function do ?**
**5- Type w[grep(pattern="mRNA", x=w)] and w[w == "mRNA"]**
<br> Is there a difference between the two outputs?
<details>
<summary>
correction
</summary>
```{r}
w[grep(pattern="mRNA", w)]
w[w == "mRNA"]
# no difference between the outputs
```
</details>
**6- Now type w[grep(pattern="RNA", w)] and w[w == "RNA"]**
<br> Is there a difference between the two outputs?
<details>
<summary>
correction
</summary>
```{r}
w[grep(pattern="RNA", w)]
w[w == "RNA"]
# grep outputs 5 values but == outputs none
```
</details>
What is the difference between **==** and **grep** ?
<details>
<summary>
correction
</summary>
>= looks for exact matches.
<br>
grep looks for **patterns**.
</details>
**7- Create vector g as:**
```{r}
g <- c("hsa-let-7a", "hsa-mir-1", "CLC", "DKK1", "LPA")
```
How many elements do w and g contain?
<details>
<summary>
correction
</summary>
```{r}
length(w); length(g)
```
</details>
**8- Do vectors w and g have the same length? Use the function identical() to check this.**
<details>
<summary>
correction
</summary>
```{r}
identical(x=length(w), y=length(g))
```
</details>
**9- Name the elements of g using the elements of w.**<br>
(i.e. the names of each element of g will be the elements of w).
<details>
<summary>
correction
</summary>
```{r}
names(g) <- w
```
</details>
**If you have time**, continue with Exercise 3b below.<br>
### Exercise 3b.
**1- Use the sub() function to replace miRNA with microRNA in the *names* of g.**
<details>
<summary>
correction
</summary>
```{r}
names(g) <- sub(pattern="miRNA", replacement="microRNA", x=names(g))
```
</details>
**2- Count how many microRNAs and mRNAs there are in g based on the column names.**
<details>
<summary>
correction
</summary>
```{r}
table(names(g))
```
</details>
**3- Create vector tt as:**
```{r}
tt <- "Introduction to R course"
```
How many characters does tt contain? Use nchar().
<details>
<summary>
correction
</summary>
```{r}
nchar(tt)
```
</details>
**4- Remove "Introduction to R" from tt.**
<br>You can try with either substr() or gsub()
<details>
<summary>
correction
</summary>
```{r}
substr(x=tt, start=17, stop=nchar(tt))
gsub(pattern="Introduction to R", replacement="", x=tt)
```
</details>