-
Notifications
You must be signed in to change notification settings - Fork 3
/
1-7_Common_Objects_answers.qmd
278 lines (173 loc) · 6.13 KB
/
1-7_Common_Objects_answers.qmd
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
# Common Objects - Answers {.unnumbered}
```{r setup, include=FALSE}
htmltools::tagList(rmarkdown::html_dependency_font_awesome())
```
```{r, echo = FALSE, purl = FALSE}
knitr::knit_hooks$set(purl = knitr::hook_purl)
options(purl = FALSE)
knitr::opts_chunk$set(purl = FALSE)
```
```{r, include = FALSE}
knitr::opts_hooks$set(eval = function(opt) {
if (any(opt$exercise))
opt$eval <- opt$include <- FALSE
opt
})
static <- TRUE
options(width = 100)
```
```{r packages, include = FALSE}
library(kableExtra)
library(knitr)
```
```{r load_data, context="data", include=FALSE}
library(survival)
```
## Preface {data-progressive="FALSE"}
Open Rstudio to do the practicals. Note that tasks with \* are optional.
### R packages
In this practical, a number of R packages are used. The packages used (with versions that were used to generate the solutions) are:
- `survival` (version: `r packageVersion("survival")`)
*`r R.version.string`*
### Dataset {.tabset .tabset-fade .tabset-pills}
For this practical, we will use the **heart** and **retinopathy** data sets from the `survival` package. More details about the data sets can be found in:
https://stat.ethz.ch/R-manual/R-devel/library/survival/html/heart.html
https://stat.ethz.ch/R-manual/R-devel/library/survival/html/retinopathy.html
Always check your data.
::: {.panel-tabset .nav-pills}
#### Task 1
Explore the **heart** and **retinopathy** data sets - print the first six and last six rows.
#### Hint 1
Use the functions head(...) and tail(...) to investigate the data set. Replace the dots with the name of the data set.
#### Solution 1
```{r data-solution, solution = TRUE}
library(survival)
head(heart)
tail(heart)
head(retinopathy)
tail(retinopathy)
```
:::
## Common R Objects
It is important to distinguish the different object in R.
### Vectors {.tabset .tabset-fade .tabset-pills}
Let's investigate some vectors.
::: {.panel-tabset .nav-pills}
#### Task 1
View the vectors `event` and `age` from the **heart** data set.
#### *Hint 1*
Use the dollar sign to select the variables.
#### Solution 1
```{r vec-solution, solution = TRUE}
heart$event
heart$age
```
:::
::: {.panel-tabset .nav-pills}
#### Task 2
View the vectors `eye` and `risk` from the **retinopathy** data set.
#### *Hint 2*
Use the dollar sign to select the variables.
#### Solution 2
```{r vec2-solution, solution = TRUE}
retinopathy$eye
retinopathy$risk
```
:::
::: {.panel-tabset .nav-pills}
#### Task 3
Create a numerical vector that consists of the values: 34, 24, 19, 23, 16. Give the name `numbers` to this new vector.
#### *Hint 3*
Use the c(...) function. Replace the dots with the numbers.
#### Solution 3
```{r vec3-solution, solution = TRUE}
numbers <- c(34, 24, 19, 23, 16)
```
:::
::: {.panel-tabset .nav-pills}
#### Task 4
Create a numerical vector that takes the integer values from 1 until 200. Give the name `numbers_2` to this new vector.
#### *Hint 4*
Use the c(...) function. Replace the dots with the numbers.
#### Solution 4
```{r vec4-solution, solution = TRUE}
numbers_2 <- c(1:200)
```
:::
::: {.panel-tabset .nav-pills}
#### Task 5 \*
Create a categorical vector that consists of the values: yes, yes, no, no, no, yes. Give the name `treatment` to this new vector.
#### *Hint 5* \*
Use the c(...) function. Replace the dots with the categories.
#### Solution 5 \*
```{r vec5-solution, solution = TRUE}
treatment <- c("yes", "yes", "no", "no", "no", "yes")
```
:::
### Matrices and Data Frames {.tabset .tabset-fade .tabset-pills}
Let's investigate some matrices and data frames.
::: {.panel-tabset .nav-pills}
#### Task 1
Create a matrix using the vectors `id` and `age` from the **heart** data set. This matrix should have 2 columns where each column represents each variable.
#### *Hint 1*
Use the function matrix(...).
#### Solution 1
```{r mat-solution-1, solution = TRUE}
matrix(c(heart$id, heart$age), , 2)
```
:::
::: {.panel-tabset .nav-pills}
#### Task 2
Create a data frame using the vectors `id`, `type` and `trt` from the **retinopathy** data set. This data frame should have 3 columns, where each column represents each variable.
#### *Hint 2*
Use the function data.frame(...).
#### Solution 2
```{r dfc-solution, solution = TRUE}
data.frame(id = retinopathy$id, type = retinopathy$type, trt = retinopathy$trt)
```
:::
### Arrays {.tabset .tabset-fade .tabset-pills}
Let's investigate some arrays.
::: {.panel-tabset .nav-pills}
#### Task 1
Create an array that consists of 2 matrices. Matrix 1 will consist of the values 1:4 and matrix 2 will consist of the values 5:8. Both matrices will have 2 columns and 2 rows.
#### *Hint 1*
Use the function array(...).
#### Solution 1
```{r ar-solution-1, solution = TRUE}
array(data = 1:8, dim = c(2, 2, 2))
```
:::
::: {.panel-tabset .nav-pills}
#### Task 2 \*
Give the name `ar1` to the previous array. Furthermore, investigate the argument dimnames and change the names of the rows, columns and matrices.
#### *Hint 2* \*
Use the function array(...). Check the help page for the dimnames argument. Note that this must be in a list format.
#### Solution 2 \*
```{r ar-solution-2, solution = TRUE}
ar1 <- array(data = 1:8, dim = c(2, 2, 2),
dimnames = list(c("Row1", "Row2"), c("Col1", "Col2"), c("Mat1", "Mat2")))
```
:::
### Lists {.tabset .tabset-fade .tabset-pills}
Let's investigate some lists.
::: {.panel-tabset .nav-pills}
#### Task 1
Create a list using the vectors `stop` from the **heart** data set and `id`, `risk` from the **retinopathy** data set. Give the names `stop_heart`, `id_reti` and `risk_reti`.
#### *Hint 1*
Use the function list(...).
#### Solution 1
```{r list-solution, solution = TRUE}
list(stop_heart = heart$stop, id_reti = retinopathy$id, risk_reti = retinopathy$risk)
```
:::
::: {.panel-tabset .nav-pills}
#### Task 2 \*
Create a list using the vectors `numbers`, `numbers_2` and `treatment`. These variables can be taken from the exercise called `Vectors`. Give the names: `numbers`, `many_numbers` and `treatment`.
#### *Hint 2* \*
Use the function list(...).
#### Solution 2 \*
```{r list2-solution, solution = TRUE}
list(numbers = numbers, many_numbers = numbers_2, treatment = treatment)
```
:::