-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobjects_practical.qmd
312 lines (185 loc) · 8.05 KB
/
objects_practical.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
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
# Objects Practical
```{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)
```
## R as a calculator (expressions and assignments)
#### a) Expressions {.unnumbered}
Use R as a calculator to calculate the following values:
* $17 ^4$,
* $45 - 2 \times 3$
* $(45 - 2) \times 3$
Enter the folowing in an R-script and then run the lines using {{< kbd Ctrl-Enter >}}.
```{r eval=FALSE, include=TRUE}
17^4
2^(1/3)
45-2 * 3
(45-2) * 3
```
#### b) Assignment {.unnumbered}
```{r}
a <- 45
b <- 2
c <- 3
d <- (a - b) * c
```
`a`, `b`, `c`, and `d` are variables. You can just type the variable name (e.g. `a`) and hit {{< kbd Ctrl-Enter >}} or use the command `print(a)`.
<br>
<!--
#### c {.unnumbered}
Use the operators `\` and `%\%` to do the following:
* Calculate the remainder after dividing 29,079 into 184,277,809.
* How many times does 29,079 go into 184,277,809 (i.e.\ what's the ``integer divide'' value)?
-->
## Math using vectors and matrices (recycling)
Define `x` and `A` as:
```{r}
#| eval: false
#| quiet: true
x <- c(1, 2, 3, 4, 5, 6)
A <- matrix(c(1, 2, 3, 4, 5, 6),nrow = 2, ncol = 3)
```
What are
```{r}
#| eval: false
#| quiet: true
x + 4
x + x
2 * x
x / c(2, 3, 4, 5)
```
```{r}
#| eval: false
#| quiet: true
A + 4
A + x
2 * A
x / c(2, 3)
```
## Common R Objects
It is important to distinguish the different object in R. Therefore we will try and create some vectors, matrices, data frames, arrays and lists. We will do this using some data from the survival package.
### Introduction to the data
For this part of the 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>
::: panel-tabset
#### a {.unnumbered}
Load the survival package to access the data sets. Explore the **heart** and **retinopathy** data sets - print the first six and last six rows (use the head and tail functions).
#### Hint a {.unnumbered}
Use the functions head(...) and tail(...) to investigate the data set. Replace the dots with the name of the data set.
:::
### Vectors
::: panel-tabset
Let's investigate some vectors.
#### a {.unnumbered}
View the vectors `event` and `age` from the **heart** data set.
#### Hint a {.unnumbered}
Use the dollar sign to select the variables.
:::
::: panel-tabset
#### b {.unnumbered}
View the vectors `eye` and `risk` from the **retinopathy** data set.
#### Hint b {.unnumbered}
Use the dollar sign to select the variables.
:::
::: panel-tabset
#### c {.unnumbered}
Create a numerical vector that consists of the values: 34, 24, 19, 23, 16. Give the name `numbers` to this new vector.
#### Hint c {.unnumbered}
Use the c(...) function. Replace the dots with the numbers.
:::
::: panel-tabset
#### d {.unnumbered}
d Create a numerical vector that takes the integer values from 1 until 200. Give the name `numbers_2` to this new vector.
#### Hint d {.unnumbered}
Use the colon operator `:` or seq function to create the vector. Use `?:` or `?seq` if needed.
:::
::: panel-tabset
#### e {.unnumbered}
Create a character vector that consists of the values: yes, yes, no, no, no, yes. Give the name `treatment` to this new vector.
#### Hint e {.unnumbered}
Use the c(...) function. Replace the dots with the categories.Do not forget the quotes `"`.
:::
### Matrices and Data Frames
Let's investigate some matrices and data frames.
::: panel-tabset
#### a {.unnumbered}
Create a matrix like this
```{r}
#| quiet: true
x <- matrix(8:11, nrow = 6, ncol = 4)
x
```
Why is `x` filled in the way it is? Hint: read about the arguments for `matrix`!
#### Hint a {.unnumbered}
How many elements does a matrix of the indicated dimensions have. How many elements does the data vector have? Find out what is done in this case. Are rows or columns filled first?
:::
::: panel-tabset
#### b {.unnumbered}
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 b {.unnumbered}
First combine the vectors using `c`. Now use the function matrix(...) to convert it to a matrix and specify that it should have two columns.
:::
::: panel-tabset
#### c {.unnumbered}
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 c {.unnumbered}
Use the function data.frame(...).
:::
### Arrays
Let's investigate arrays.
::: panel-tabset
#### a {.unnumbered}
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 a {.unnumbered}
Use the function array(...).
:::
::: panel-tabset
#### b {.unnumbered}
Give the name `ar1` to the previous array. Furthermore, investigate the argument dimnames and change the names of the rows, columns and matrices.
#### Hint b {.unnumbered}
Use the function array(...). Check the help page for the dimnames argument. Note that this must be in a list format.
:::
### Lists
Let's investigate lists.
::: panel-tabset
#### a {.unnumbered}
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 a {.unnumbered}
Use the function list(...).
:::
::: panel-tabset
#### b {.unnumbered}
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 b {.unnumbered}
Use the function `list(...)` using named function arguments.
:::
## Saving files and data
Do the following to practice saving and opening files in R.
1) Look at the variables (or other objects) that are stored in your Workspace by typing either `objects()` or `ls()`
2) Check your working directory by typing `getwd()`. Now change it to a different directory - preferably your own flash drive - by using the function `setwd()`, for example:
```{r}
#| eval: false
setwd("C:/Users/Elizabeth/My Documents/R Course")
```
3) Use the function `save.image()` to save your `R` session to a file called `YourLastName_practical1.RData` (replace YourLastName with your last name). Note that this will save a `.RData` file that contains only those objects you see when you run `ls()`. It does not save any code you typed into the console or into the source pane.
4) Use the RStudio 'File' drop-down menu to save your R source code to a file called `YourLastName\_practical1.R` (replace YourLastName with your last name). Note that this will only save the text you've typed into the source pane. It does not save any objects or anything typed into or ran through the console.
5) Use the function `save()` to save only the objects `numbers`, `numbers_2` and `treatment` from the 'Vectors' exercise.
to a file called `YourLastName\_objects.RData` (replace YourLastName with your last name).
6) Now close out RStudio entirely, select ``Save'' or ``Yes'' in any dialog boxes that pop up, and then reopen RStudio. Is your source code still there?
7) Run `ls()`; are your objects still there?
8) You can change these kinds of options by going to `Tools - Global Options`. Go there and deselect 'Restore .RData into workspace at startup'. Then close RStudio and choose to save the `.RData` file.
9) Reopen RStudio; your environment should be empty. Load your objects back in using `load()` (e.g. `Ribble\_practical1.RData`") and then run `ls()` again. Do you see your objects now?
10) Check what the working directory is by again running `getwd()` - has it been reset?