-
Notifications
You must be signed in to change notification settings - Fork 1
/
exercise6_old.Rmd
314 lines (222 loc) · 5.87 KB
/
exercise6_old.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 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>
*Answer*
</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/biocorecrg/CRG_RIntroduction/trunk/i_o_files.tar.gz")
# extract archive (tar: archive, gz: compressed)
system(command="tar -xvzf i_o_files.tar.gz")
# list files present in i_o_files
dir("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>
*Answer*
</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". ?sort**
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
zsorted <- sort(z)
```
</details>
**4- Write zsorted content into file ex6a_output.txt. ?write**
<details>
<summary>
*Answer*
</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 does the file look now?**
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
write(zsorted, "ex6a_output.txt", ncolumns=1)
```
</details>
### Exercise 6b - I/O on data frames: play with the arguments of read.table
**1- field separator**
* **Read ex6b_IO_commas_noheader.txt in object fs. ?read.table.
What are the dimensions of fs?**
<details>
<summary>
*Answer*
</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>
*Answer*
</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>
*Answer*
</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 the appropriate one.**
<details>
<summary>
*Answer*
</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>
*Answer*
</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>
*Answer*
</summary>
```{r, eval=F, message=F, warning=F, error=F}
sk <- read.table("i_o_files/ex6b_IO_skip.txt")
```
</details>
**Is R complaining ?**<br>
Check "manually" the file (open it 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 in the file again.**<br>
<details>
<summary>
*Answer*
</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>
*Answer*
</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>
*Answer*
</summary>
```{r, eval=F, warning=F, message=F, error=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 is the "comment.char" argument used for ? **Adjust the comment.char argument and read in the file again.**
<details>
<summary>
*Answer*
</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>
*Answer*
</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>
*Answer*
</summary>
```{r, eval=F, warning=F, error=F, message=F}
fin <- read.table("i_o_files/ex6b_IO_final.txt")
```
</details>
* **Adjust the appropriate arguments of "write.table" according to what you have learnt so far, in order to obtain the data frame "fin" of dimensions 167 x 4.**
<details>
<summary>
*Answer*
</summary>
```{r, eval=F}
fin <- read.table("i_o_files/ex6b_IO_final.txt",
sep=",",
header=TRUE,
skip=3,
comment.char="&"
)
```
</details>