-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexercise5.Rmd
374 lines (278 loc) · 7.52 KB
/
exercise5.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
## Exercise 5. Data frame manipulation
Create the script "exercise5.R" and save it to the "Rcourse/Module1" directory: you will save all the commands of exercise 5 in that script.
<br>Remember you can comment the code using #.
<details>
<summary>
correction
</summary>
```{r, eval=F}
getwd()
setwd("Rcourse/Module1")
setwd("~/Rcourse/Module1")
```
</details>
### Exercise 5a
**1- Create the following data frame:**
|43|181|M|
|34|172|F|
|22|189|M|
|27|167|F|
<br>
With Row names: John, Jessica, Steve, Rachel.
<br>
And Column names: Age, Height, Sex.
<details>
<summary>
correction
</summary>
```{r, eval=F}
df <- data.frame(Age=c(43, 34, 22, 27),
Height=c(181, 172, 189, 167),
Sex=c("M", "F", "M", "F"),
row.names = c("John", "Jessica", "Steve", "Rachel"),
stringsAsFactors=FALSE)
```
</details>
**2- Check the structure of df with str().**
<details>
<summary>
correction
</summary>
```{r, eval=F}
str(df)
```
</details>
**3- Calculate the average age and height in df**
Try different approaches:
* Calculate the average for each column separately.
<details>
<summary>
correction
</summary>
```{r, eval=F}
mean(df$Age)
mean(df$Height)
```
</details>
* Calculate the average of both columns simultaneously using the apply() function.
<details>
<summary>
correction
</summary>
```{r, eval=F}
# we have to remove the Sex column: we can calculate the average only with numbers
apply(df[,-3], 2, mean)
apply(df[,1:2], 2, mean)
apply(df[,-grep("Sex", colnames(df))], 2, mean)
```
</details>
**4- Add one row to df2: Georges who is 53 years old and 168 tall.**
<details>
<summary>
correction
</summary>
```{r, eval=F}
# Georges= allows us to enter the row name at the same time as we add a row
df <- rbind(df, Georges=c(53, 168, "M"))
```
</details>
**5- Change the row names of df so the data becomes anonymous:**
Use Patient1, Patient2, etc. instead of actual names.
<details>
<summary>
correction
</summary>
```{r, eval=F}
rownames(df) <- c("Patient1", "Patient2", "Patient3", "Patient4", "Patient5")
# try also the paste function!
rownames(df) <- paste("Patient", 1:5, sep="")
```
</details>
**6- Create the data frame df2 that is a subset of df which will contain only the female entries.**
<details>
<summary>
correction
</summary>
```{r, eval=F}
# which elements are female ("F" in the "Sex" colum)
df$Sex=="F"
# retrieve rows that contain the female entries, and save in df2
df2 <- df[df$Sex=="F",]
```
</details>
**7- Create the data frame df3 that is a subset of df which will contain only entries of males taller than 170.**
<details>
<summary>
correction
</summary>
```{r, eval=F}
# which entries are males
df$Sex=="M"
# which entries are greater than 170 in column "Height"
df$Sex=="M" & df$Height > 170
# retrieve rows that contain the males that are taller than 170, and save in df3
df3 <- df[df$Sex=="M" & df$Height > 170,]
```
</details>
### Exercise 5b
**1. Create two data frames mydf1 and mydf2 as:**
mydf1:
|1|14|
|2|12|
|3|15|
|4|10|
mydf2:
|1|paul|
|2|helen|
|3|emily|
|4|john|
|5|mark|
With column names: **"id", "age"** for mydf1, and **"id", "name"** for mydf2.
<details>
<summary>
correction
</summary>
```{r, eval=F}
mydf1 <- data.frame(id=1:4, age=c(14,12,15,10))
mydf2 <- data.frame(id=1:5, name=c("paul", "helen", "emily", "john", "mark"))
```
</details>
**2- Merge mydf1 and mydf2 by their "id" column.**
Look for the help page of **merge** and/or Google it!
<details>
<summary>
correction
</summary>
```{r, eval=F}
# input 2 data frames
# "by" columns indicate by which column you want to merge the data
merge(x=mydf1, y=mydf2, by.x="id", by.y="id")
mydf3 <- merge(x=mydf1, y=mydf2, by="id")
```
</details>
**3- Order mydf3 by decreasing age.**
Look for the help page of **order**.
<details>
<summary>
correction
</summary>
```{r, eval=F}
# order the age column (default is increasing order)
order(mydf3$age)
# order the age column by decreasing order
order(mydf3$age, decreasing = TRUE)
# order the whole data frame by the column age in decreasing order
mydf3[order(mydf3$age, decreasing = TRUE), ]
```
</details>
### Exercise 5c
**1- Using the download.file function, download [this file](https://public-docs.crg.es/biocore/sbonnin/Rcourse/genes_dataframe.RData) to your current directory.** (Right click on "this file" -> Copy link location to get the full path).
<details>
<summary>
correction
</summary>
```{r, eval=F}
# failing: download.file("https://github.com/sbcrg/CRG_RIntroduction/blob/master/genes_dataframe.RData", "genes_dataframe.RData")
download.file("https://public-docs.crg.es/biocore/sbonnin/Rcourse/genes_dataframe.RData", "genes_dataframe.RData")
```
</details>
**2- The function dir() lists the files and directories present in the current directory: check if genes_dataframe.RData was copied.**
<details>
<summary>
correction
</summary>
```{r, eval=F}
dir()
```
</details>
**3- Load genes_dataframe.RData in your environment**
Use the *load* function.
<details>
<summary>
correction
</summary>
```{r, eval=F}
load("genes_dataframe.RData")
```
</details>
**4- genes_dataframe.RData contains the df_genes object: is it now present in your environment?**
<details>
<summary>
correction
</summary>
```{r, eval=F}
ls()
```
</details>
**5- Explore df_genes and see what it contains**
You can use a variety of functions: str, head, tail, dim, colnames, rownames, class...
<details>
<summary>
correction
</summary>
```{r, eval=F}
str(df_genes)
head(df_genes)
tail(df_genes)
dim(df_genes)
colnames(df_genes)
rownames(df_genes)
class(df_genes)
```
</details>
**6- Select rows for which pvalue_KOvsWT < 0.05 AND log2FoldChange_KOvsWT > 0.5. Store in the up object.**
<details>
<summary>
correction
</summary>
```{r, eval=F}
# rows where pvalue_KOvsWT < 0.05
df_genes$pvalue_KOvsWT < 0.05
# rows where log2FoldChange_KOvsWT > 0.5
df_genes$log2FoldChange_KOvsWT > 0.5
# rows that comply both of the above conditions
df_genes$pvalue_KOvsWT < 0.05 & df_genes$log2FoldChange_KOvsWT > 0.5
# select rows for which pvalue_KOvsWT < 0.05 AND log2FoldChange_KOvsWT > 0.5
up <- df_genes[df_genes$pvalue_KOvsWT < 0.05 &
df_genes$log2FoldChange_KOvsWT > 0.5,]
```
</details>
How many rows (genes) were selected?
**7- Select from the up object the Zinc finger protein coding genes (i.e. the gene symbol starts with Zfp). Use the grep() function.**
<details>
<summary>
correction
</summary>
```{r, eval=F}
# extract gene symbol column
up$gene_symbol
# use grep to get the genes matching the pattern "Zfp"
up[grep("Zf", up$gene_symbol), ]
```
</details>
**8- Select rows for which pvalue_KOvsWT < 0.05 AND log2FoldChange_KOvsWT is > 0.5 OR < -0.5.**
For the selection of log2FoldChange: give the **abs** function a try!
<br>Store in the diff_genes object.
<details>
<summary>
correction
</summary>
```{r, eval=F}
# rows where pvalue_KOvsWT < 0.05
df_genes$pvalue_KOvsWT < 0.05
# rows where log2FoldChange_KOvsWT > 0.5
df_genes$log2FoldChange_KOvsWT > 0.5
# rows where log2FoldChange_KOvsWT < -0.5
df_genes$log2FoldChange_KOvsWT > -0.5
# rows where log2FoldChange_KOvsWT < -0.5 OR log2FoldChange_KOvsWT > 0.5
df_genes$log2FoldChange_KOvsWT > 0.5 | df_genes$log2FoldChange_KOvsWT > -0.5
# same as above but using the abs function
abs(df_genes$log2FoldChange_KOvsWT) > 0.5
# combine all required criteria
df_genes$pvalue_KOvsWT < 0.05 & abs(df_genes$log2FoldChange_KOvsWT) > 0.5
# extract corresponding entries
diff_genes <- df_genes[df_genes$pvalue_KOvsWT < 0.05 &
abs(df_genes$log2FoldChange_KOvsWT) > 0.5,]
```
</details>
How many rows (genes) were selected?