-
Notifications
You must be signed in to change notification settings - Fork 10
/
slide_r_elements_1.Rmd
413 lines (304 loc) · 7.41 KB
/
slide_r_elements_1.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
---
title: "Variables, Data types & Operators"
subtitle: "Elements of the R language"
author: "Marcin Kierczak & Nima Rafati"
keywords: bioinformatics, course, scilifelab, nbis, R
output:
xaringan::moon_reader:
encoding: 'UTF-8'
self_contained: false
chakra: 'assets/remark-latest.min.js'
css: 'assets/slide.css'
lib_dir: libs
include: NULL
nature:
ratio: '4:3'
highlightLanguage: r
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
slideNumberFormat: "%current%/%total%"
---
exclude: true
count: false
```{r,echo=FALSE,child="assets/header-slide.Rmd"}
```
<!-- ------------ Only edit title, subtitle & author above this ------------ -->
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE, width=60)
```
```{r,include=FALSE}
# load the packages you need
#library(dplyr)
#library(tidyr)
#library(stringr)
#library(ggplot2)
#library(mkteachr)
```
---
name: intro
# Introduction
Today, we will talk about various elements of a programming language and see how they are realized in R.
# Contents
- **variables and their types**
- **operators**
- vectors
- numbers as vectors
- strings as vectors
- matrices
- lists
- data frames
- objects
- repeating actions: iteration and recursion
- decision taking: control structures
- functions in general
- variable scope
- core functions
---
name: variables
# Variables
- Creating a variable is simply assigning a name to some structure that stores data...
```{r variables,echo=TRUE,size='Tiny'}
7 + 9
a <- 7
a
b <- 9
b
c <- a + b
c
```
---
# Variables cted.
We are not constrained to numbers...
```{r variables2,echo=TRUE}
text1 <- 'a'
text2 <- 'qwerty'
text1
text2
```
# Is `<-` equivalent to `=`? Which one shall I use?
- `val <- 3`, `val = 3` and `3 -> val` are three equivalent ways of assigning in R, But, it's best to use only `<-` to avoid possible confusion. The equal sign `=` should be used when setting function arguments ie; `f(a = 3)`.
---
name: variables_naming
# Variables — naming conventions
- How to write variable names?
- What is legal/valid?
- What is a good style?
--
A syntactically valid name consists of letters, numbers and the dot or underline characters and starts with a letter or the dot not followed by a number.
--
Names such as ".2way" are not valid, and neither are the so-called *reserved words*.
--
# Reserved words
- `if, else, repeat, while, function, for, in, next, break, TRUE, FALSE, NULL, Inf, NaN, NA, NA_integer_, NA_real_, NA_complex_, NA_character_`
- and you also **cannot** use: `c, q, t, C, D, I`
- and you **should not** use: `T, F`
---
name: variables_good_style
# Variables — good style
- So, how to name variables?
- make them informative, e.g. `genotypes` instead of `fsjht45jkhsdf4`,
--
- use consistent notation across your code – the same *naming convention*,
--
- camelNotation vs. dot.notation vs. dash_notation,
--
- do not `give_them_too_long_names`,
--
- in the dot notation avoid `my.variable.2`, use `my.variable2` instead,
--
- there are certain customary names:
- `tmp` - for temporary variables;
- `cnt` for counters;
- `i,j,k` within loops,
- `pwd` - for password...
---
name: data_types_1
# Variables have types
A *numeric* stores numbers of different *types*:
```{r variable.types1,echo=TRUE}
x <- 41.99 # assign 41.99 to x
typeof(x)
```
---
name: class_type_mode
# Classes, types, and modes
- `class` what type of object is it for R,
- `typeof()` what R thinks it is,
- `mode()` how S language would see it (backward compatibility),
- `storage.mode()` how is it stored in the memory; useful when talking to C or Java,
```{r class,echo=TRUE,eval=TRUE}
x <- 1:3
class(x)
typeof(x)
mode(x)
storage.mode(x)
```
---
name: type_casting
# Type casting
By default, any *numeric* is stored as *double* !
```{r variable types2,echo=TRUE}
y=12 # now assign an integer value to y
class(y) # still numeric
typeof(y) # an integer, but still a double!
```
But we can explicitly **cast it** to integer:
```{r variable types3,echo=TRUE}
x <- as.integer(x) # type conversion, casting
typeof(x)
class(x)
is.integer(x)
```
> We need **casting** because sometimes a function requires data of certain type!
---
name: casting2
# More on type casting
Be careful when casting!
```{r careful.casting,echo=TRUE}
pi <- 3.1415926536 # assign approximation of pi to pi
pi
pi <- as.integer(pi) # not-so-careful casting
pi
pi <- as.double(pi) # trying to rescue the situation
pi
```
Casting is not rounding!
```{r casting.not.rounding,echo=TRUE}
as.integer(3.14)
as.integer(3.51)
```
---
name: ceiling_floor
# Ceiling, floor and a round corner
```{r casting.not.rounding2,echo=TRUE}
floor(3.51) # floor of 3.51
ceiling(3.51) # ceiling of 3.51
round(3.51, digits=1) # round to one decimal
```
---
name: coercion
# What happens if we cast a string to a number
```{r casting.string,echo=TRUE}
as.numeric('4.5678')
as.double('4.5678')
as.numeric('R course is cool!')
```
---
name: some_special_values
# Special values
```{r special.values,echo=TRUE}
-1/0 # Minus infinity
1/0 # Infinity
```
and also:
```{r special.values2,echo=TRUE}
112345^67890 # Also infinity for R
1/2e78996543 # Zero for R
Inf - Inf # Not a Number
```
---
name: logical_type
# Logical type
```{r logical.type,echo=TRUE}
a <- 7 > 2
b <- 2 >= 7
a
b
class(a)
typeof(a)
```
R has three logical values: TRUE, FALSE and NA.
```{r logical.type2,echo=TRUE}
x <- c(TRUE, FALSE, NA)
names(x) <- as.character(x)
and_truth_table <- outer(x, x, "&") # AND table
```
```{r logical.DT, echo=FALSE}
kableExtra::kable(and_truth_table)
```
---
name: logical_cted
# Logical type cted.
```{r logical.type3,echo=TRUE}
x <- TRUE
x
x <- T # also valid
x
is.logical(x)
typeof(x)
```
- Observe that in R the logical type is also a numeric!
```{r logical.as.number,echo=TRUE}
x <- TRUE
y <- FALSE
x + y
2 * x
x * y
```
---
name: logical_joke
# A trap set up for you
Never ever use variable names as T or F. Why?
```{r TFtrap,echo=TRUE}
F <- T
T
F
```
Maybe applicable in politics, but not really in science...
---
name: character_type
# Character type
It is easy to work with characters and strings:
```{r char.type,echo=TRUE}
character <- 'c'
text <- 'This is my first sentence in R.'
text
character
class(character)
typeof(text) # also of 'character' type
```
---
name: character_type_2
# Character type
```{r char.type2,echo=TRUE}
number <- 3.14
number.text <- as.character(number) # cast to char
number.text
class(number.text)
as.numeric(number.text) # and the other way round
```
---
name: basic_string_ops
# Basic string operations
```{r basic.string,echo=TRUE}
text1 <- "John had a yellow "
text2 <- "submarine"
result <- paste(text1, text2, ".", sep='')
result
sub("submarine", "cab", result)
substr(result, start=1, stop=5)
```
---
name: basic_printing
# Basic printing
```{r basic.printing,echo=TRUE}
txt <- "blue"
(txt <- 'red')
val <- 345.78
sprintf("The weight of a %s ball is %g g", txt, val)
```
<!-- --------------------- Do not edit this and below --------------------- -->
---
name: end_slide
class: end-slide, middle
count: false
# See you at the next lecture!
```{r, echo=FALSE,child="assets/footer-slide.Rmd"}
```
```{r,include=FALSE,eval=FALSE}
# manually run this to render this document to HTML
#rmarkdown::render("presentation_demo.Rmd")
# manually run this to convert HTML to PDF
#pagedown::chrome_print("presentation_demo.html",output="presentation_demo.pdf")
```