-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession2.Rmd
420 lines (278 loc) · 10.4 KB
/
session2.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
---
title: "R through the user interface of RStudio - performing basic operations"
subtitle: "Learning the basics of R - Part 1"
author:
- "Ernest Guevarra"
date: '23 October 2024'
output:
xaringan::moon_reader:
css: xaringan-themer.css
nature:
slideNumberFormat: "%current%"
highlightStyle: github
highlightLines: true
ratio: 16:9
countIncrementalSlides: true
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(
fig.width=9, fig.height=3.5, fig.retina=3,
out.width = "100%",
cache = FALSE,
echo = TRUE,
message = FALSE,
warning = FALSE,
hiline = TRUE
)
if (!require(remotes)) install.packages("remotes")
if (!require(fontawesome)) remotes::install_github("rstudio/fontawesome")
```
```{r xaringan-themer, include=FALSE, warning=FALSE}
library(xaringanthemer)
style_mono_light(
base_color = "#002147",
title_slide_background_image = "",
title_slide_background_size = "cover",
header_font_google = google_font("Fira Sans"),
text_font_google = google_font("Fira Sans Condensed"),
text_font_size = "1.2em",
link_color = "#214700",
header_h1_font_size = "50px",
header_h2_font_size = "40px",
header_h3_font_size = "30px",
code_font_google = google_font("Fira Mono"),
text_slide_number_font_size = "0.5em",
footnote_font_size = "0.5em"
)
```
# Outline
1. Using RStudio to access the R console
2. Some basic operators in R
* Arithmetic
* Relational
* Logical
* Assignment
3. Using RStudio to create scripts
---
# Using RStudio to access the R console
* RStudio has a specific window/pane for the R console which behaves exactly the same as the raw R console
* Issue commands directly on the console to produce a desired outcome or perform a specific action
* Most commands produce an output that is shown on the console
---
class: inverse, center, middle
# Basic operators in R
---
# Arithmetic operators
These operators are used to carry out mathematical operations like addition and multiplication. Here is a list of arithmetic operators available in R.
```{r, echo = FALSE}
operator <- c("+", "-", "*", "/", "^", "%%", "%/%")
description <- c("Addition", "Subtraction", "Multiplication", "Division", "Exponent", "Modulus", "Integer Division")
data.frame(operator, description) |>
knitr::kable(
row.names = FALSE,
col.names = c("Operator", "Description")
) |>
kableExtra::kable_styling(
bootstrap_options = "striped",
full_width = FALSE,
position = "center"
)
```
---
# Arithmetic operators - application
Let us try R's arithmetic operations to calculate BMI:
$$ \text{BMI} ~ = ~ \frac{kgs}{m ^ 2} $$
using the following values:
$$ \text{weight} ~ = ~ 80 ~ \text{kgs} $$
$$ \text{height} ~ = ~ 1.6 ~ \text{metres} $$
---
# Arithmetic operators - application
```{r, echo = TRUE}
80 / 1.6 ^ 2
```
---
# Relational operators
Relational operators are used to compare between values. Here is a list of relational operators available in R.
```{r, echo = FALSE}
operator <- c("<", ">", "<=", ">=", "==", "!=")
description <- c("Less than", "Greater than", "Less than or equal to", "Greater than or equal to", "Equal to", "Not equal to")
data.frame(operator, description) |>
knitr::kable(
row.names = FALSE,
col.names = c("Operator", "Description")
) |>
kableExtra::kable_styling(
bootstrap_options = "striped",
full_width = FALSE,
position = "center"
)
```
---
# Relational operators - application
Let us apply the relational operators using again BMI as an example.
Say we have **Person A** with a weight of *80 kilograms* and a *height of 1.6 metres* and **Person B** with a weight of *120 kilograms* and a height of *210 centimetres*.
In R, try to answer the following questions:
1. Is **Person A** heavier in weight compared to **Person B**?
2. Is **Person A** taller in height compared to **Person B**?
3. Whose BMI is greater, **Person A** or **Person B**?
---
# Relational operators - application
### 1. Is **Person A** heavier in weight compared to **Person B**?
```{r, echo = TRUE}
80 > 120 ## Is Person A's weight greater than Person B's weight
80 < 120 ## Is Person A's weight lesser than Person B's weight
80 == 120 ## Is Person A's weight the same as Person B's weight
```
---
# Relational operators - application
### 2. Is **Person A** taller in height compared to **Person B**?
```{r, echo = TRUE}
1.6 > 210 / 100 ## Is Person A's height greater than Person B's height
1.6 < 210 / 100 ## Is Person A's height lesser than Person B's height
1.6 == 210 / 100 ## Is Person A's height the same as Person B's height
```
---
# Relational operators - application
### 3. Whose BMI is greater, **Person A** or **Person B**?
```{r, echo = TRUE}
80 / 1.6 ^ 2 > 120 / (210 / 100) ^ 2 ## Is Person A's BMI greater than Person B's BMI
80 / 1.6 ^ 2 < 120 / (210 / 100) ^ 2 ## Is Person A's BMI lesser than Person B's BMI
80 / 1.6 ^ 2 == 120 / (210 / 100) ^ 2 ## Is Person A's BMI the same as Person B's BMI
```
---
# Logical operators
Logical operators are used to carry out Boolean operations like AND, OR etc.
```{r, echo = FALSE, eval = FALSE}
operator <- c("!", "&", "&&", "|", "||")
description <- c("Logical NOT", "Element-wise logical AND", "Logical AND", "Element-wise logical OR", "Logical OR")
data.frame(operator, description) |>
knitr::kable(
row.names = FALSE,
col.names = c("Operator", "Description")
) |>
kableExtra::kable_styling(
bootstrap_options = "striped",
full_width = FALSE,
position = "center"
)
```
Operator | Description
:--- | :---
! | Logical NOT
& | Element-wise logical AND
&& | Logical AND
| | Element-wise logical OR
|| | Logical OR
---
# Logical operators - application
Let us apply the logical operators again using the example of BMI for Person A and Person B in the previous exercise.
In R, answer the following questions using logical operators:
1. Is the weight of **Person A** AND the weight of **Person B** both equal to 80 kilograms?
2. Is the weight of **Person A** OR the weight of **Person B** less than 100 kilograms?
3. Is the weight of **Person A** greater than the weight of **Person B** AND the height of **Person A** greater than the height of **Person B**?
4. Is the weight of **Person A** greater than the weight of **Person B** OR the height of **Person A** greater than the height of **Person B**?
---
# Logical operators - application
### 1. Is the weight of **Person A** AND the weight of **Person B** both equal to 80 kilograms?
```{r, echo = TRUE}
80 == 80 & 120 == 80
```
### 2. Is the weight of **Person A** OR the weight of **Person B** less than 100 kilograms?
```{r, echo = TRUE}
80 < 100 | 120 < 100
```
---
# Logical operators - application
### 3. Is the weight of **Person A** greater than the weight of **Person B** AND the height of **Person A** greater than the height of **Person B**?
```{r, echo = TRUE}
80 > 120 & 1.6 > 2.1
```
### 4. Is the weight of **Person A** greater than the weight of **Person B** OR the height of **Person A** greater than the height of **Person B**?
```{r, echo = TRUE}
80 > 120 | 1.6 > 2.1
```
---
# Assignment operators
These operators are used to assign values to objects.
```{r, echo = FALSE}
operator <- c("<-", "<<-", "=", "->", "->>")
description <- c("Leftwards assignment", "Leftwards assignment", "Leftwards assignment", "Rightwards assignment", "Rightwards assignment")
data.frame(operator, description) |>
knitr::kable(
row.names = FALSE,
col.names = c("Operator", "Description")
) |>
kableExtra::kable_styling(
bootstrap_options = "striped",
full_width = FALSE,
position = "center"
)
```
---
# Assignment operators - application
Let us again use the BMI example to apply the assignment operators:
1. Assign the weight of person A to an object named `weight_a`
2. Assign the height of person A to an object named `height_a`
3. Calculate BMI for person A using objects `weight_a` and `height_a`. Assign the value of BMI to an object named `bmi_a`.
---
# Assignment operators - application
### 1. Assign the weight of person A to an object named `weight_a`
```{r, echo = TRUE}
weight_a <- 80
weight_a
```
### 2. Assign the height of person A to an object named `height_a`
```{r, echo = TRUE}
height_a <- 1.6
height_a
```
---
# Assignment operators - application
### 3. Calculate BMI for person A using objects `weight_a` and `height_a`. Assign the value of BMI to an object named `bmi_a`.
```{r, echo = TRUE}
bmi_a <- weight_a / height_a ^ 2
bmi_a
```
---
class: inverse, center, middle
# Questions?
---
class: inverse, center, middle
# Practical session
Using what we have learned in the first half of the session, we'll learn about how to write R scripts.
---
# Using RStudio to create scripts
* So far, we have tried issuing commands in R straight into the console to perform single commands at a time
* In real life context, we will rarely use R for a single command. To make meaningful analysis, we will often string together a series of commands to produce an intended result/output
* We will also often have to repeat the same commands with different data or parameters
* As such, direct to console issuing of commands in R will be highly inefficient
---
# Using RStudio to create scripts
.pull-left[
RStudio, being an **integrated development environment (IDE)**, provides functionality and tools for
* recording multiple lines of commands which can be run/issued onto the console line by line; and,
* saving the recorded multiple lines of code/commands for later use.
This record of multiple lines of code/commands is often called an **R script** and is saved as plain text file with a `.R` extension.
]
.pull-right[
```{r example_script, echo = TRUE, eval = FALSE}
## R script to calculate BMI of
## person A and person B
weight_a <- 80
height_a <- 1.6
bmi_a <- weight_a / height_a ^ 2
weight_b <- 12
height_b <- 2.1
bmi_b <- weight_b / height_b ^ 2
```
]
---
class: inverse, center, middle
# Questions?
---
class: inverse, center, middle
# Thank you!
Slides can be viewed at https://oxford-ihtm.io/open-reproducible-science/session2.html
PDF version of slides can be downloaded at https://oxford-ihtm.io/open-reproducible-science/pdf/session2-r-basics-part1.pdf
R scripts for slides available [here](https://github.com/OxfordIHTM/open-reproducible-science/blob/main/session2.Rmd)