-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path10-refresher.Rmd
178 lines (128 loc) · 5.81 KB
/
10-refresher.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
# R refresher {#sec-refresher}
The objectives of this chapter is to review some R syntax, functions
and data structures that will be needed for the following chapters.
## Administration
- Setting up an [RStudio project](https://uclouvain-cbio.github.io/WSBIM1207/sec-startr.html#getting-set-up)
- [Install](https://uclouvain-cbio.github.io/WSBIM1207/sec-startr.html#r-packages)
packages from CRAN and
[Bioconductor](https://uclouvain-cbio.github.io/WSBIM1207/sec-bioinfo.html#sec-bioconductor).
```{r, eval = FALSE}
BiocManager::install("UCLouvain-CBIO/rWSBIM1322")
```
- [Avoid saving and loading your
workspace](https://uclouvain-cbio.github.io/WSBIM1207/sec-startr.html#getting-set-up)
(the `.RData` file).
- [UTF-8 character encoding](https://uclouvain-cbio.github.io/WSBIM1207/sec-startr.html#getting-set-up).
- Starting a markdown document.
## Basic data structures and operations
- [vectors](https://uclouvain-cbio.github.io/WSBIM1207/sec-startr.html#introduction-to-r),
generating and subsetting vectors.
- Missing values.
- [Factors](https://uclouvain-cbio.github.io/WSBIM1207/sec-startdata.html#factors)
- [Dataframes](https://uclouvain-cbio.github.io/WSBIM1207/sec-startr.html#introduction-to-r) (and tibbles)
- [Matrices](https://uclouvain-cbio.github.io/WSBIM1207/sec-startdata.html#matrices)
- Arrays
- [Lists](https://uclouvain-cbio.github.io/WSBIM1207/sec-startdata.html#lists)
**Summary**
| | number of dimensions | number of data types |
|------------+----------------------+----------------------|
| **vector** | 1 (length) | 1 |
| **matrix** | 2 | 1 |
| **array** | n | 1 |
| **dataframe** | 2 | n |
| **list** | 1 (length) | n |
## Tidyverse
- The [dplyr](https://uclouvain-cbio.github.io/WSBIM1207/sec-dplyr.html) package
- [Piping](https://uclouvain-cbio.github.io/WSBIM1207/sec-dplyr.html#pipes)
- Wide and long data, and their conversion with the `pivot_longer` and
`pivot_wider` functions.
## Saving and exporting
- `saveRDS()` and `readRDS()` binary data.
- [Exporting
data](https://uclouvain-cbio.github.io/WSBIM1207/sec-dplyr.html#exporting-data-1)
with `write.csv` and `read.csv` (or `write_csv` and `read_csv`) and
same for other types of spreadsheets.
- [Saving
figures](https://uclouvain-cbio.github.io/WSBIM1207/sec-vis.html#exporting-plots)
(`ggsave` and file devices such as `png()`, `pdf()`, ...).
- Package versions: `sessionInfo()`
## Programming
- [Writing
functions](https://uclouvain-cbio.github.io/WSBIM1207/sec-prog.html#writing-new-functions)
- [Conditionals](https://uclouvain-cbio.github.io/WSBIM1207/sec-prog.html#conditionals)
`if`/`else`
- [Iteration](https://uclouvain-cbio.github.io/WSBIM1207/sec-prog.html#iteration):
`for` loops and `apply` functions
## Additional exercises
`r msmbstyle::question_begin()`
Complete the following function. It is supposed to take two inputs,
`x` and `y` and, depending whether the `x > y` or `x <= y`, it
generates the permutation `sample(x, y)` in the first case or draws a
sample from `rnorm(1000, x, y)` in the second case. Finally, it
returns the sum of all values.
```{r, eval = FALSE}
fun <- function(x, y) {
res <- NA
if () { ## 1
res <- sample(,) ## 2
} else {
res <- rnorm(, , ) ## 3
}
return() ## 4
}
```
`r msmbstyle::question_end()`
`r msmbstyle::question_begin()`
Read the `interro2.rds` from the `rWSBIM1207` package (version 0.1.9
of later) file into R. The path to the file can be found with the
`rWSBIM1207::interro2.rds()` function.
This dataframe provides the scores for 4 tests for 10 students.
- Write a function that calculates the average score for the 3 best
tests.
- Calculate this average score for the 10 students.
This can be done using the `apply` function or using `dplyr`
functions. For the latter, see also `rowwise()`.
Note the situation of students that have only presented 3 out of
4 tests (i.e they have a `NA` for one test). It is up to you to decide
whether you simply take the mean of the 3, or whether you prefer to
drop the worst of 3 and calculate the mean of the 2 best marks. Make
sure you are aware of what your implementation returns and, ideally,
state it explicitly in your response.
`r msmbstyle::question_end()`
```{r, include = FALSE}
stopifnot(packageVersion("rWSBIM1207") >= "0.1.9")
moy <- function(x) {
x <- sort(x, decreasing = TRUE)[1:3] ## gets rid of NAs
mean(x, na.rm = TRUE)
}
interro2 <- readRDS(rWSBIM1207::interro2.rds())
interro2$m <- apply(interro2[, -1], 1, moy)
interro2 <- readRDS(rWSBIM1207::interro2.rds())
library("tidyverse")
interro2 |>
rowwise() |>
mutate(m = moy(c(interro1, interro2, interro3, interro4)))
interro2 |>
pivot_longer(names_to = "interro",
values_to = "score", -noma) |>
group_by(noma) |>
summarise(m = moy(score)) |>
full_join(interro2)
```
`r msmbstyle::question_begin()`
- Create a matrix of dimenions 100 by 100 containing data from a
normal distribution of mean 0 and standard deviation 1.
- Compute the means of each row and each column using the `apply()`
and `rowMeans()`/`colMeans()` functions. Confirm that both provide
the same results.
- Compute the difference between the column means and the row
means. Does the result make sense?
`r msmbstyle::question_end()`
`r msmbstyle::question_begin()`
- Using the data `kem2_se` data from the `rWSBIM1322` package, compute
de delta values or each gene (delta is the difference between the
highest and lowest values). To do this, write a function `delta()`
that takes a vector of numerics as input and returns the delta
value, and apply it on the object's assay.
- Re-use your `delta()` function and apply it on each sample.
`r msmbstyle::question_end()`