-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexercise2.Rmd
313 lines (221 loc) · 4.33 KB
/
exercise2.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
## Exercise 2. Numeric vector manipulation
### Exercise 2a.
Create the script "exercise2.R" and save it to the "Rcourse/Module1" directory: you will save all the commands of exercise 2 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 a numeric vector y which contains the numbers from 2 to 11, both included.**
<br>Show y in the terminal.
<details>
<summary>
correction
</summary>
```{r}
y <- c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
# same as
y <- 2:11
# show in terminal:
y
```
</details>
**3- How many elements are in y? I.e what is the length of vector y ?**
<details>
<summary>
correction
</summary>
```{r}
length(y)
```
</details>
**4- Show the 2nd element of y.**
<details>
<summary>
correction
</summary>
```{r}
y[2]
```
</details>
**5- Show the 3rd and the 6th elements of y.**
<details>
<summary>
correction
</summary>
```{r}
y[c(3,6)]
```
</details>
**6- Remove the 4th element of y: reassign. What is now the length of y ?**
<details>
<summary>
correction
</summary>
```{r}
# remove 4th element and reassign
y <- y[-4]
# length of y
length(y)
```
</details>
**7- Show all elements of y that are less than 7.**
<details>
<summary>
correction
</summary>
```{r}
# which elements of y are less than 7:
y < 7
# show those elements
y[ y < 7 ]
```
</details>
**8- Show all elements of y that are greater or equal to 4 and less than 9.**
<details>
<summary>
correction
</summary>
```{r}
y[ y >= 4 & y < 9 ]
```
</details>
**9- Create the vector x of 1000 random numbers from the normal distribution:**
<br>***First read the help page of the rnorm() function.***
<details>
<summary>
correction
</summary>
```{r}
# help page for the rnorm function
help(rnorm)
# produce a vector of 1000 random numbers from the normal distribution
x <- rnorm(1000)
```
</details>
**10. What are the mean, median, minimum and maximum values of x?**
<details>
<summary>
correction
</summary>
```{r}
mean(x); median(x); min(x); max(x)
```
</details>
**11- Run the summary() function on x. <br>What additional information do you obtain ?**
<details>
<summary>
correction
</summary>
```{r}
summary(x)
```
</details>
**12- Create vector y2 as:**<br>
```{r}
y2 <- c(1, 11, 5, 62, 18, 2, 8)
```
**13. What is the sum of all elements in y2 ?**
<details>
<summary>
correction
</summary>
```{r}
sum(y2)
```
</details>
**14- Which elements of y2 are also present in y ?
<br>Note: remember the %in% operator.**
<details>
<summary>
correction
</summary>
```{r}
y2[ y2 %in% y ]
```
</details>
**15- Multiply each element of y2 by 1.5: reassign.**
<details>
<summary>
correction
</summary>
```{r}
y2 <- y2 * 1.5
```
</details>
**16- Use the function any() to check if the number 3 is present.**
<details>
<summary>
correction
</summary>
```{r}
# "Given a set of logical vectors, is at least one of the values true?"
any( y2 == 3 )
```
</details>
### Exercise 2b.
**1- Create the vector myvector as:**
```{r}
myvector <- c(1, 2, 3, 1, 2, 3, 1, 2, 3)
```
**Create the same vector using the rep() function (?rep)**
<details>
<summary>
correction
</summary>
```{r}
myvector <- rep(1:3, 3)
```
</details>
**2- Reassign the 5th, 6th and 7th position of myvector with the values 8, 12 and 32, respectively.**
<details>
<summary>
correction
</summary>
```{r}
# reassign one by one
myvector[5] <- 8
myvector[6] <- 12
myvector[7] <- 32
# or reassign all at once
myvector[5:7] <- c(8, 12, 32)
```
</details>
**3- Calculate the fraction/percentage of each element of myvector (relative to the sum of all elements of the vector).
<br>sum() can be useful.**
<details>
<summary>
correction
</summary>
```{r}
# sum of all elements of the vector
mytotal <- sum(myvector)
# divide each element by the sum
myvector / mytotal
# multiply by 100 to get a percentage
(myvector / mytotal) * 100
```
</details>
**4- Add vector c(2, 4, 6, 7) to myvector (combining both vectors): reassign!**
<details>
<summary>
correction
</summary>
```{r}
# create the new vector
newvector <- c(2, 4, 6, 7)
# combine both myvector and newvector
c(myvector, newvector)
# reassign myvector
myvector <- c(myvector, newvector)
```
</details>