-
Notifications
You must be signed in to change notification settings - Fork 10
/
slide_r_elements_3.Rmd
681 lines (502 loc) · 14.4 KB
/
slide_r_elements_3.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
---
title: "Matrices, Data Frames, and Lists"
subtitle: "R Foundations for Data Analysis"
author: "Marcin Kierczak, Guilherme Dias"
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"}
```
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
---
name: contents
# Contents of the lecture
- variables and their types
- operators
- vectors
- numbers as vectors
- strings as vectors
- **matrices**
- **data frames**
- **lists**
<!-- - **objects** -->
- repeating actions: iteration and recursion
- decision taking: control structures
- functions in general
- variable scope
- core functions
---
name: matrices
# Matrices
A **matrix** is a 2-dimensional data structure. Like vectors, it consists of elements of the same type. A matrix has *rows* and *columns*.
Say, we want to construct this matrix in R:
$$\mathbf{X} = \left[\begin{array}
{rrr}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{array}\right]$$
```{r matrix, echo=T}
X <- matrix(1:9, # a sequence of numbers to fill in
nrow=3, # three rows (alt. ncol=3)
byrow=T) # populate matrix by row
X
```
---
name: matrices_dim
# Matrices — dimensions
To check the dimensions of a matrix, use `dim()`:
```{r matrix.dim, echo=T}
X
dim(X) # 3 rows and 3 columns
```
---
name: matrices_indexing
# Matrices — indexing
Elements of a matrix are retrieved using the `[]` notation.
We have to specify 2 dimensions -- the rows and the columns:
$$\mathbf{X} = \left[\begin{array}
{rrr}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{array}\right]$$
```{r matrix.ind, echo=T}
X[1,2] # Retrieve element from the 1st row, 2nd column
X[3,] # Retrieve the entire 3rd row
X[,2] # Retrieve the 2nd column
```
---
name: matrices_indexing_2
exclude: true
# Matrices — indexing cted.
$$\mathbf{X} = \left[\begin{array}
{rrr}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{array}\right]$$
```{r matrix.ind2, echo=T}
X[c(1,3),] # Retrieve rows 1 and 3
X[c(1,3),c(3,1)]
```
---
name: matrices_oper_1
# Matrices — operations
Usually the functions that work for a vector also work for matrices.
To order a matrix with respect to, say, 2nd column:
```{r matrix.oper2, echo=T}
X <- matrix(sample(1:9,size = 9), nrow = 3)
ord <- order(X[,2])
X[ord,]
```
---
name: matrices_t
# Matrices — transposition
To **transpose** a matrix use `t()`:
```{r matrix.t, echo=T}
X
t(X)
```
---
name: matrices_oper_2
exclude: true
# Matrices — operations 2
To get the diagonal of the matrix:
```{r matrix.diag, echo=T}
X
diag(X) # get values on the diagonal
```
---
name: matrices_tri
exclude: true
# Matrices — operations, triangles
To get the upper or the lower triangle use `upper.tri()` and `lower.tri()` respectively:
```{r matrix.tri, echo=T}
X # print X
upper.tri(X) # which elements form the upper triangle
X[upper.tri(X)] <- 0 # set them to 0
X # print the new matrix
```
---
name: matrices_multi
# Matrices — multiplication
Different types of matrix multiplication exist:
```{r matrix.multi, echo=T}
A <- matrix(1:4, nrow = 2, byrow=T)
B <- matrix(5:8, nrow = 2, byrow=T)
A * B # Hadamard product
A %*% B # Matrix multiplication
# A %x% B # Kronecker product
# A %o% B # Outer product (tensor product)
```
---
name: matrices_outer
exclude: true
# Matrices — outer
Outer product can be useful for generating names
```{r matrix.outer1, echo=T}
outer(letters[1:4], LETTERS[1:4], paste, sep="-")
```
---
name: matrices_expand_grid
exclude: true
# Expand grid
But `expand.grid()` is more convenient when you want, e.g. generate combinations of variable values:
```{r matrix.expand.grid, echo=T}
expand.grid(height = seq(120, 121),
weight = c('1-50', '51+'),
sex = c("Male","Female"))
```
---
name: matrices_apply
# Matrices — apply
Function `apply` is a very useful function that applies a given function to either each value of the matrix or in a column/row-wise manner. Say, we want to have mean of values by column:
```{r matrix.apply, echo=T}
X
apply(X, MARGIN=2, mean) # MARGIN=1 would do it for rows
```
---
name: matrices_apply_2
exclude: true
# Matrices — apply cted.
And now we will use `apply()` to calculate for each element in a matrix its deviation from the mean squared:
```{r matrix.apply2, echo=T}
X
my.mean <- mean(X)
apply(X, MARGIN=c(1,2),
function(x, my.mean) (x - my.mean)^2, my.mean)
```
---
name: matrices_colSums
# Matrices — useful fns.
While `apply()` is handy, it is a bit slow and for the most common statistics, there are special functions col/row Sums/Means:
```{r matrix.colSums, echo=T}
X
colMeans(X)
```
These functions are faster!
---
name: matrices_add_row_col
# Matrices — adding rows/columns
To add rows or columns to a matrix; or to make a matrix out of two or more vectors of equal length:
```{r matrix.binding, echo=T}
x <- c(1,1,1)
y <- c(2,2,2)
cbind(x,y)
rbind(x,y)
```
---
name: matrices_arrays
exclude: true
# Matrices — more dimensions
```{r matrix.multidim, echo=T}
dim(Titanic)
```
--
exclude: true
```{r matrix.Titanic.plot, echo=F, message=F, fig.width = 6, fig.height = 3.5, dpi=120}
library(vcd)
mosaic(Titanic, gp_labels=gpar(fontsize=7))
```
---
name: data_frames_1
# Data frames
- **Data frames** are also two-dimensional data structures.
- Different columns can have different data types!
- Technically, a data frame is just a list of vectors.
--
<br/><br/>
<center>
.size-70[
![](images/data_frame.png)
]
</center>
---
name: data_frames_create
# Data frames — creating a data frame
```{r data.frame.create, echo=T}
df <- data.frame(c(1:5),
LETTERS[1:5],
c(T,F,F,T,T))
df
```
---
name: data_frames_columns
# Data frames — name your columns!
- Always try to give meaningful names to your columns
```{r data.frame.name, echo=T}
df <- data.frame(numbers=c(1:5),
letters=c('a','b','c','d','e'),
logical=c(T,F,F,T,T))
df
```
---
name: data_frames_accessing
# Data frames — accessing values
- We can always use the `[row,column]` notation to access values inside data frames.
```{r data.frame.access, echo=T}
df[1,] # get the first row
df[,2] # the second column
df[2:3, 'letters'] # get rows 2-3 from the 'letters' column
```
---
name: data_frames_dollar
# Data frames — accessing values
- We can also use dollar sign `$` to access columns
```{r data.frame.dollar, echo=T}
df$letters # get the column named 'letters'
df$letters[2:3] # get the second and third elements of the column named 'letters'
```
---
name: data_frames_factors_1
exclude: true
# Data frames — factors
An interesting observation:
```{r data.frame.factor, echo=T, eval=F}
df$letter
df$letter <- as.character(df$letter)
df$letter
```
---
name: data_frames_factors_2
exclude: true
# Data frames — factors cted.
To treat characters as characters at data frame creation time, one can use the **stringsAsFactors** option set to TRUE:
```{r data.frame.factor2, echo=T, eval=F}
df <- data.frame(no=c(1:5),
letter=c("a","b","c","d","e"),
isBrown=sample(c(TRUE, FALSE),
size = 5,
replace=T),
stringsAsFactors = TRUE)
df$letter
```
Well, as you see, it did not work as expected...
---
name: data_frames_names
# Data frames — names
To get or change row/column names:
```{r data.frame.names, echo=T}
colnames(df) # get column names
colnames(df) <- c('num','let','logi') # assign column names
colnames(df)
rownames(df) # get row names
rownames(df) <- letters[1:5] # assign row names
rownames(df)
```
---
name: data_frames_merging
# Data frames — merging
We can merge two data frames on certain a key using `merge()`:
```{r data.frame.merge, echo=T}
age <- data.frame(ID=c(1:4),
age=c(37,48,22,NA))
clinical <- data.frame(ID=c(1:4),
status=c("sick","healthy","healthy","sick"))
patients <- merge(age, clinical, by='ID')
patients
```
---
name: data_frames_summarizing
# Data frames — summarising
To get an overview of the data in each column, use `summary()`:
```{r data.frame.summary, echo=T}
summary(patients)
```
---
name: data_frames_missing
# Data frames — missing data
We can use functions to deal with missing values:
```{r data.frame.missing, echo=T}
is.na(patients) # check where the NAs are
na.omit(patients) # remove all rows containing NAs
patients[rowSums(is.na(patients)) > 0,] # select rows containing NAs
```
---
name: lists_1
# Lists — collections of various data types
A list is a collection of elements:
```{r lists, echo=T}
bedr <- data.frame(product = c("POANG", "MALM", "RENS"),
type = c("chair", "bed", "rug"),
price = c(1200, 2300, 899))
rest <- data.frame(dish = c("kottbullar", "daimtarta"),
price = c(89, 32))
park <- 162
ikea_uppsala <- list(bedroom = bedr,
restaurant = rest,
parking = park)
str(ikea_uppsala) # str (structure) of an object
```
---
name: lists_subsetting_double
# Subsetting lists
We can access elements of a list using the `[[]]` notation.
```{r lists_subsetting_double, echo=T}
ikea_uppsala[[2]]
class(ikea_uppsala[[2]])
```
---
name: lists_subsetting_single
# Subsetting lists — .cted
What if we use `[]`? We get a list back!
```{r lists_subsetting_single, echo=T}
ikea_uppsala[2]
class(ikea_uppsala[2])
```
--
- A piece of a list is still a list! Use `[[]]` to pull out the actual data.
---
name: lists_subsetting_names
# Subsetting lists — using names
If the elements of a list are named, we can also use the `$` notation:
```{r lists_subsetting_names, echo=T}
ikea_uppsala$restaurant
ikea_uppsala$restaurant$price
```
---
name: lists_nested
# Lists inside lists
We can use lists to store hierarchies of data:
```{r lists_nested, echo=T}
ikea_lund <- list(parking = 125)
ikea_sweden <- list(ikea_lund = ikea_lund,
ikea_uppsala = ikea_uppsala)
# use names to navigate inside the hierarchy
ikea_sweden$ikea_lund$parking
ikea_sweden$ikea_uppsala$parking
```
---
name: objects_type_class
exclude: true
# Objects — type vs. class
An object of class **factor** is internally represented by numbers:
```{r obj.type.class, echo=T, eval=F}
size <- factor('small')
class(size) # Class 'factor'
mode(size) # Is represented by 'numeric'
typeof(size) # Of integer type
```
---
name: objects_str
exclude: true
# Objects — structure
Many functions return **objects**. We can easily examine their **structure**:
```{r obj.str, echo=T, fig.height=3.7, fig.width=3}
his <- hist(1:5, plot=F)
str(his)
object.size(hist) # How much memory the object consumes
```
---
name: objects_fix
exclude: true
# Objects — fix
We can easily modify values of object's **attributes**:
```{r obj.fix, echo=T, eval=F}
attributes(his)
attr(his, "names")
#fix(his) # Opens an object editor
```
---
name: objects_lists_as_S3
exclude: true
# Lists as S3 classes
A list that has been named, becomes an S3 class:
```{r obj.S3.list, echo=T, eval=F}
my.list <- list(numbers = c(1:5),
letters = letters[1:5])
class(my.list)
class(my.list) <- 'my.list.class'
class(my.list) # Now the list is of S3 class
```
However, that was it. We cannot enforce that *numbers* will contain numeric values and that *letters* will contain only characters. S3 is a very primitive class.
---
name: objects_S3
exclude: true
# S3 classes
For an S3 class we can define a *generic function* applicable to all objects of this class.
```{r obj.S3, echo=T, eval=F}
print.my.list.class <- function(x) {
cat('Numbers:', x$numbers, '\n')
cat('Letters:', x$letters)
}
print(my.list)
```
But here, we have no error-proofing. If the object will lack *numbers*, the function will still be called:
```{r obj.S3.error, echo=T, eval=F}
class(his) <- 'my.list.class' # alter class
print(his) # Gibberish but no error...
```
---
name: objects_generics
exclude: true
# S3 classes — still useful?
Well, S3 class mechanism is still in use, esp. when writing **generic** functions, most common examples being *print* and *plot*. For example, if you plot an object of a Manhattan.plot class, you write *plot(gwas.result)* but the true call is: *plot.manhattan(gwas.result)*. This makes life easier as it requires less writing, but it is up to the function developers to make sure everything works!
---
name: objects_S4
exclude: true
# S4 class mechanism
S4 classes are more advanced as you actually define the structure of the data within the object of your particular class:
```{r S4, echo=T, eval=F}
setClass('gene',
representation(name='character',
coords='numeric')
)
my.gene <- new('gene', name='ANK3',
coords=c(1.4e6, 1.412e6))
```
---
name: objects_S4_slots
exclude: true
# S4 class — slots
The variables within an S4 class are stored in the so-called **slots**. In the above example, we have 2 such slots: *name* and *coords*. Here is how to access them:
```{r S4.slots, echo=T, eval=F}
my.gene@name # access using @ operator
my.gene@coords[2] # access the 2nd element in slot coords
```
---
name: objects_S4_methods
exclude: true
# S4 class — methods
The power of classes lies in the fact that they define both the data types in particular slots and operations (functions) we can perform on them. Let us define a *generic print function* for an S4 class:
```{r S4.methods, echo=T, eval=F}
setMethod('print', 'gene',
function(x) {
cat('GENE: ', x@name, ' --> ')
cat('[', x@coords, ']')
})
print(my.gene) # and we use the newly defined print
```
<!-- --------------------- 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")
```