-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexercise6.Rmd
535 lines (385 loc) · 9.42 KB
/
exercise6.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
## Exercise 6.
Create the script "exercise6.R" and save it to the "Rcourse/Module2" directory: you will save all the commands of exercise 6 in that script.
<br>Remember you can comment the code using #.
<details>
<summary>
correction
</summary>
```{r, eval=F}
getwd()
setwd("Rcourse/Module2")
setwd("~/Rcourse/Module2")
```
</details>
### Exercise 6a. Input / output
**1- Download folder "i_o_files" in your current directory with:**
```{r, eval=F}
# system invokes the OS command specified by the "command" argument.
system(command="svn export https://github.com/sarahbonnin/CRG_RIntroduction/trunk/i_o_files")
```
All files that will be used for exercise 6 are found in the **i_o_files** folder !
**2- Read in the content of ex6a_input.txt using the scan command; save in object z**
How many elements are in z?
<details>
<summary>
correction
</summary>
```{r, eval=F}
# scan content of the file
z <- scan("i_o_files/ex6a_input.txt")
# number of elements (length of vector)
length(z)
```
</details>
**3- Sort z: save sorted vector in object "zsorted".**
<details>
<summary>
correction
</summary>
```{r, eval=F}
zsorted <- sort(z)
```
</details>
**4- Write zsorted content into file ex6a_output.txt.**
<details>
<summary>
correction
</summary>
```{r, eval=F}
write(zsorted, "ex6a_output.txt")
```
</details>
**5- Check the file you produced in the RStudio file browser (click on the file in bottom-right panel "Files" tab). Save the content of zsorted again but this time setting the argument "ncolumns" to 1: how is the file different?**
<details>
<summary>
correction
</summary>
```{r, eval=F}
write(zsorted, "ex6a_output.txt", ncolumns=1)
```
</details>
### Exercise 6b - I/O on data frame: play with the arguments of read.table
**1- field separator**
* Read ex6b_IO_commas_noheader.txt in object fs.
What are the dimensions of fs?
<details>
<summary>
correction
</summary>
```{r, eval=F}
# read in file with default parameters
fs <- read.table("i_o_files/ex6b_IO_commas_noheader.txt")
dim(fs)
```
</details>
* Fields/columns are separated by commas: change the default value of the "sep" argument and read in the file again.
What are now the dimensions of fs?
<details>
<summary>
correction
</summary>
```{r, eval=F}
# change field separator to ","
fs <- read.table("i_o_files/ex6b_IO_commas_noheader.txt",
sep=",")
dim(fs)
```
</details>
**2- field separator + header**
* Read ex6b_IO_commas_header.txt in object fs_c.
What are the dimensions of fs_c ?
<details>
<summary>
correction
</summary>
```{r, eval=F}
fs_c <- read.table("i_o_files/ex6b_IO_commas_header.txt")
dim(fs_c)
```
</details>
* Check head(fs_c) and change the default field separator to an appropriate one.
<details>
<summary>
correction
</summary>
```{r, eval=F}
fs_c <- read.table("i_o_files/ex6b_IO_commas_header.txt",
sep=",")
```
</details>
* The first row should to be the header (column names): change the default value of the header parameter and read in the file again.
What are now the dimensions of fs_c ?
<details>
<summary>
correction
</summary>
```{r, eval=F}
fs_c <- read.table("i_o_files/ex6b_IO_commas_header.txt",
sep=",",
header=TRUE)
```
</details>
**3- skipping lines**
* Read ex6b_IO_skip.txt in object sk.
<details>
<summary>
correction
</summary>
```{r, eval=F}
sk <- read.table("i_o_files/ex6b_IO_skip.txt")
```
</details>
Is R complaining ?<br>
Check "manually" the file (in the R Studio file browser).<br>
* The skip argument allows you to ignore one or more line(s) before reading in a file. Introduce this argument with the appropriate number of lines to skip, and read the file again.<br>
<details>
<summary>
correction
</summary>
```{r, eval=F}
sk <- read.table("i_o_files/ex6b_IO_skip.txt",
skip=2)
dim(sk)
```
</details>
* Is R still complaining?
What are now the dimensions of sk ?
* Change the default field separator.
What are now the dimensions of sk ?
<details>
<summary>
correction
</summary>
```{r, eval=F}
sk <- read.table("i_o_files/ex6b_IO_skip.txt",
skip=2,
sep=",",
header=T)
```
</details>
**4- Comment lines**
* Read ex6b_IO_comment.txt in object cl.
<details>
<summary>
correction
</summary>
```{r, eval=F}
cl <- read.table("i_o_files/ex6b_IO_comment.txt")
```
</details>
Is R complaining again ? Check manually the file and try to find out what is wrong...<br>
What os the comment.char argument used for ? Adjust the comment.char argument and read the file again.
<details>
<summary>
correction
</summary>
```{r, eval=F}
cl <- read.table("i_o_files/ex6b_IO_comment.txt",
comment.char = "*")
```
</details>
* Adjust also the header and sep arguments to read in the file correctly.
What are now the dimensions of cl?
<details>
<summary>
correction
</summary>
```{r, eval=F}
cl <- read.table("i_o_files/ex6b_IO_comment.txt",
comment.char = "*",
sep=",",
header=TRUE)
dim(cl)
```
</details>
**4- final**
* Read ex6b_IO_final.txt in object fin.
<details>
<summary>
correction
</summary>
```{r, eval=F}
fin <- read.table("i_o_files/ex6b_IO_final.txt")
```
</details>
* Adjust the appropriate parameters according to what you have learnt, in order to obtain the data frame "fin" of dimensions 167 x 4.
<details>
<summary>
correction
</summary>
```{r, eval=F}
fin <- read.table("i_o_files/ex6b_IO_final.txt",
sep=",",
header=TRUE,
skip=3,
comment.char="&"
)
```
</details>
### Exercice 6c - I/O on a data frame
**1- Read in file ex6c_input.txt in ex6 object**
Warning: the file has a header !
<br>
Check the structure of ex6 (remember the **str** command).
<details>
<summary>
correction
</summary>
```{r, eval=F}
ex6 <- read.table("i_o_files/ex6c_input.txt",
header=TRUE)
str(ex6)
```
</details>
**2- Now read in the same file but, this time, set the argument as.is to TRUE.**
Check again the structure: what has changed ?
<details>
<summary>
correction
</summary>
```{r, eval=F}
ex6 <- read.table("i_o_files/ex6c_input.txt",
header=TRUE,
as.is=TRUE)
str(ex6)
```
</details>
**3- What are the column names of ex6 ?**
<details>
<summary>
correction
</summary>
```{r, eval=F}
colnames(ex6)
```
</details>
**4- Change the name of the first column of ex6 from "State" to "Country".**
<details>
<summary>
correction
</summary>
```{r, eval=F}
# extract all column names of ex6
colnames(ex6)
# extract the name of the first column only
colnames(ex6)[1]
# reassign name of the first column only
colnames(ex6)[1] <- "Country"
```
</details>
**5- How many countries are in the Eurozone, according to ex6 ?**
Remember the table function.
<details>
<summary>
correction
</summary>
```{r, eval=F}
table(ex6$Eurozone)
```
</details>
**6- In the Eurozone column: change "TRUE" with "yes" and "FALSE" with "no".**
<details>
<summary>
correction
</summary>
```{r, eval=F}
# select the Eurozone column
ex6$Eurozone
# elements of the Eurozone column that are exactly TRUE
ex6$Eurozone==TRUE
# extract actual values that are TRUE
ex6$Eurozone[ex6$Eurozone==TRUE]
# reassign all elements that are TRUE with "yes"
ex6$Eurozone[ex6$Eurozone==TRUE] <- "yes"
# same with FALSE
ex6$Eurozone[ex6$Eurozone==FALSE] <- "no"
```
</details>
**7- In the column Country: how many country names from the list contain the letter "c" (capital- or lower-case) ?**
Remember the grep function. Check the help page.
<details>
<summary>
correction
</summary>
```{r, eval=F}
# country names with "c" (lower-case)
grep("c", ex6$Country)
# country names with "c" or "C" (ignoring case)
grep("c", ex6$Country, ignore.case = TRUE)
# show actual country names
grep("c", ex6$Country, value=TRUE, ignore.case = TRUE)
```
</details>
**8- According to that data frame: how many people live:**
+ in the European union (whole table) ?
+ in the Eurozone ?
<details>
<summary>
correction
</summary>
```{r, eval=F}
# sum the whole population column
sum(ex6$Population)
# select elements of ex6 where Eurozone is "yes"
ex6$Eurozone == "yes"
# select only elements in Population for which the corresponding Eurozone elements are "yes"
ex6$Population[ex6$Eurozone == "yes"]
# sum that selection
sum(ex6$Population[ex6$Eurozone == "yes"])
```
</details>
**9- Write ex6 into file ex6c_output.txt**
*After each of the following steps, check the output file in the RStudio file browser (lower-right panel).*
* Try with the default arguments.
<details>
<summary>
correction
</summary>
```{r, eval=F}
write.table(ex6, file="ex6c_output.txt")
```
</details>
* Add the argument "row.names" set to FALSE.
<details>
<summary>
correction
</summary>
```{r, eval=F}
write.table(ex6, file="ex6c_output.txt",
row.names = FALSE)
```
</details>
* Add the argument "quote" set to FALSE.
<details>
<summary>
correction
</summary>
```{r, eval=F}
write.table(ex6, file="ex6c_output.txt",
row.names = FALSE,
quote = FALSE)
```
</details>
* Add the argument "sep" set to "\t" or to ","
<details>
<summary>
correction
</summary>
```{r, eval=F}
write.table(ex6, file="ex6c_output.txt",
row.names = FALSE,
quote = FALSE,
sep="\t")
```
</details>
<details>
<summary>
correction
</summary>
```{r, eval=F}
write.table(ex6, file="ex6c_output.txt",
row.names = FALSE,
quote = FALSE,
sep=",")
```
</details>