forked from evolgeniusteam/R-for-bioinformatics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtalk02.Rmd
694 lines (466 loc) · 11.6 KB
/
talk02.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
682
683
684
685
686
687
688
689
690
691
692
693
694
---
title: "R language basics, part 1"
subtitle: "HUST Bioinformatics course series"
author: "Wei-Hua Chen (CC BY-NC 4.0)"
institute: "HUST, China"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
beamer_presentation:
theme: AnnArbor
colortheme: beaver
fonttheme: structurebold
highlight: tango
includes:
in_header: mystyle.sty
---
```{r include=FALSE}
color_block = function(color) {
function(x, options) sprintf('\\color{%s}\\begin{verbatim}%s\\end{verbatim}',
color, x)
}
## 将错误信息用红色字体显示
knitr::knit_hooks$set(error = color_block('red'))
```
# section 1: outline
## TOC
1. 数据类型
2. 基本操作
3. 变量
4. 函数/子例程(Functions)
5. 模型和公式
6. R包
7. 获得帮助
**注**:不一定按照上述顺序
# section 2: R basics - 基本数据类型
## 基本数据类型
最基本的数据类型包括**数字**和**字符串**,是其它数据类型的基本组成部分。
数字
\FontSmall
```{r}
## 整数
287
```
```{r}
## 小数
99.99
```
```{r}
## 科学计数法
1e-3
1e2
```
## 逻辑符号
\FontSmall
真
```{r eval=FALSE}
TRUE
T
```
假
```{r eval=FALSE}
FALSE
F
```
其本质是数字
```{r}
1 + TRUE
2 * FALSE
```
## 字符串
字符串则是可以是任何字符的组合,由单引号或双引号包括。比如:
\FontSmall
```{r eval=FALSE}
'a sentence' ## 单括号
"一个字符串" ## 双括号
'1.123' ## 像是数字的字符串
'*%%*()!@##&@(9' ## 乱码
```
## 简单数据类型
简单数据类型包括vector和矩阵,它们都可以包含某一种基本数据类型的多个数值,比如由多个数字组成的矩阵,多个字符串组成的vector等。但它们**只能包含单一数据类型**;这一点稍后会有解释。
\FontSmall
```{r eval=FALSE}
c(100, 20, 30) ## 整数 vector
c("字符串", "数组","是我") ## 字符串 vector
c(TRUE, FALSE, TRUE, T, F) ## 一个逻辑 vector
```
\FontNormal
如上所示,数组通常用函数 c() 来定义。除此之外,还可以用 ':' 操作符号来定义包含连续整数的 vector:
\FontSmall
```{r}
2:8
```
## vector 的数据类型转换规则
vector只能包含一种基本数据类型。因此,在定义数组时,如果输入的数值是混合的,那么某些基本数据类型会自动转换为其它类型,以保证数值类型的一致性;这在英文里称为coerce,有强制转换的意思。这种转换的优先级为:
* 逻辑类型 -> 数字类型
* 逻辑类型 -> 字符串
* 数字类型 -> 字符串
我们可以用 `class()`或`str() ` 函数来判断 vector包含的数据类型。以后会介绍两者的不同。
## vector 的数据类型转换规则
\FontSmall
```{r}
class( c(45, TRUE, 20, FALSE, -100) ); ## 逻辑和数字类型
str( c("string a", FALSE, "string b", TRUE) ); ## 逻辑和字符
str( c("a string", 1.2, "another string", 1e-3) ); ## 数字和字符
```
## 矩阵(matrix)
矩阵也可看做是一种带有限制的二维数组,其限制是,矩阵内的数值必须是同一种类型。当输入混合有多种基本数据类型时,矩阵会按上面提到的规则进行强制转换。
矩阵由函数 `matrix()` 定义,比如:
\FontSmall
```{r}
matrix( c(20, 30.1, 2, 45.8, 23, 14), nrow = 2, byrow = T );
```
## Matrix 函数的参数
\FontSmall
```{r}
?matrix
```
## `matrix()`参数测试
\FontSmall
```{r}
matrix( c(20, 30.1, 2, 45.8, 23, 14), nrow = 2, byrow = T );
```
```{r}
matrix( c(20, 30.1, 2, 45.8, 23, 14), nrow = 2, byrow = F );
```
```{r}
matrix( c(20, 30.1, 2, 45.8, 23, 14), nrow = 2,
dimnames = list( c("row_A", "row_B"), c("A", "B", "C") ) );
```
## `matrix()`同时指定 ncol 和 nrow
矩阵的指定长度,即 nrow × ncol,可以不同于输入数据的长度。矩阵长度较小时,输入数据会被截短;而矩阵长度较大时,输入数据则会被重复使用。举例如下:
\FontSmall
```{r}
## 生成一个2x5长度为10的矩阵,但输入数据的长度为20
matrix( 1:20, nrow = 2, ncol = 5, byrow = T);
```
```{r}
## 生成一个2x3长度为6的矩阵,但输入数据长度只有3
matrix( 1:3, nrow = 2, ncol = 3, byrow = T );
```
## `matrix()`更多测试
注意:上面两种情况下,系统并不会给出任何提示。但下面两种情况,系统会报警告信息。
第一种情况,矩阵长度大于输入数据长度,且前者不是后者的整数倍。
\FontSmall
```{r}
matrix( 1:3, nrow = 2, ncol = 4, byrow = T );
```
## `matrix()`更多测试, cont.
第二种情况,矩阵长度小于输入数据的长度,且后者不是前者的整数倍。
\FontSmall
```{r}
matrix( letters[1:20], nrow = 3, ncol = 5, byrow = T );
```
## 用`? matrix ` 命令获得帮助
在 Console 窗口中输入 `? matrix ` 命令,可在**窗口4**获得有关`matrix()`函数的帮助信息:
![matrix() 函数使用方法](images/talk02/get_help_for_matrix_function.png){height=40%}
## `class()`和`str()`的区别
\FontSmall
```{r}
class(matrix( c(20, 30.1, 2, 45.8, 23, 14),
nrow = 2, byrow = T ));
```
```{r}
str(matrix( c(20, 30.1, 2, 45.8, 23, 14),
nrow = 2, byrow = T ));
```
\FontNormal
`str()`更多功用以后会详细介绍
## 数组(arrray)
vector和矩阵都是数组。vector是一维数组,矩阵是二维数组。这就意味着:a) 还可以有更多维的数组,b)高维数组与vector和矩阵一样,只能包含一种基本数据类型。高维数组可以由函数 array() 定义:
\FontSmall
```{r}
array( data = LETTERS[1:16],
dim = c(2,4,2),
dimnames = list( c("A","B"),
c("one","two","three","four"),
c("一", "二") ));
```
# section 3: R basics - 简单算术
## 运算符
### **加减乘除**老一套
\FontSmall
```{r eval=FALSE}
1 + 2 - 3 * 4 / 5; ## 加减乘除
1 + (2 - 3) * 4 / 5; ## 改变优先级
```
\FontNormal
### 进阶操作
\FontSmall
```{r eval=FALSE}
2 ^ 6; ## 阶乘
5 %% 2; ## 取余
```
\FontNormal
### 逻辑运算符
\FontSmall
```{r eval=FALSE}
T | F; ## or
T & F; ## and
5 | 0; ## == 0 FALSE, > 0 TRUE
```
\FontNormal
? `5 & -1` 结果应该是 TRUE 还是 FALSE???
## 数学函数
- log, log2, log10
- exp
- sin, cos, tan
- sqrt
## 变量与赋值
在进行下一步之前,先介绍变量与赋值的基本知识
在R中,可以用任何下面的方式**赋值**给**变量**
\FontSmall
```{r eval=FALSE}
## 常用
x <- c(10,100,1000, 10000);
## -- 也可以;
x = c(10,100,1000, 10000);
## -- 少见
c(10,100,1000, 10000) -> x;
## -- ???
assign( "x", c(10, 100, 1000, 10000) );
```
\FontNormal
其中`x`就叫做变量
**注**: 赋值后,变量内容默认不再打印到 console
**注2**: c() 又叫做 **concatenation function** (以后会再次介绍)
## 变量的管理
现在我们运行多个赋值操作:
\FontSmall
```{r echo=TRUE, message=FALSE}
x <- c(10,100,1000, 10000);
m <- matrix( c(20, 30.1, 2, 45.8, 23, 14), nrow = 2,
dimnames = list( c("row_A", "row_B"), c("A", "B", "C") ) );
```
![RStudio enviroment window](images/talk02/enviroment_screenshot.png){height=30%}
## 通过 Console window 管理变量
\FontSmall
```{r}
ls(); ## 显示当前环境下所有变量
rm( x ); ## 删除一个变量
ls();
##rm(list=ls()); ## 删除当前环境下所有变量!!!
```
## vector 算术, cont.
**vectorisation**:R最重要的一个概念
\FontSmall
```{r}
x <- c(10,100,1000, 10000);
( y <- sqrt( x ) * 4 + 10 ); ## 赋值的之后打印变量内容
```
\FontNormal
**vectorisation**的核心在于数据**自动循环使用**
\FontSmall
```{r}
x / c(10,100);
```
```{r}
x / c(10,100,1000); ## 会报错但仍会循环计算
```
## matrix 的算术
先查看 m 的内容:
\FontSmall
```{r}
m;
```
```{r}
m / 10;
m / c(1,10,100);
```
## matrix 的算术, cont.
\FontSmall
```{r}
m / c(1,10);
m / c(1,10,100,1000); ## 多于列数
```
## 更多 matrix 相关函数
**注**:其中许多也要用于其它数据类型(以后会讲到)
- dim( m );
- nrow( m );
- ncol( m );
- range( m ); ## 内容是数字时
- summary( m ); ## 也可用于 vector
## vector manipulation
### 合并
\FontSmall
```{r}
a <- 1:3;
b <- LETTERS[1:3];
( ab <- c(a,b) );
mode( ab ); ## 一个新的函数~ ...
```
## vector manipulation, cont.
### 取部分
\FontSmall
```{r}
ab[1];
ab[ c(1, 4, 5) ];
ab[ 2:5 ];
ab[ length(ab) ]; ## length 函数
```
## vector manipultaion, cont.
### 替换单个值
\FontSmall
```{r}
( ab[1] <- "一" );
```
## vector manipulation, cont.
### 替换多个值
\FontSmall
```{r}
ab[c(2,3)] <- c("二", "三");
ab
```
## vector manipulation, cont.
### naming elements & then replace a value
\FontSmall
```{r}
( names( ab ) <- as.character( ab ) ); ## 注意 names() 和 as.character() 函数的用法
ab[ c("A", "B") ] <- c("ah", "bo");
ab;
```
## vector manipulation, cont.
### other useful functions
\FontSmall
```{r}
## reverse
rev(1:10);
```
```{r}
## sort & order
lts <- sample( LETTERS[1:20] );
sort( lts );
order( lts ); ## 注意与 sort 的不同
```
## matrix manipulation
### 取一行、多行
\FontSmall
```{r}
m;
m[ 1 , ];
m[ 1:2, ];
```
## matrix manipulation, cont.
### 取一行、多行, cont.
\FontSmall
```{r}
m[ "row_A", ]
m[ c( "row_B", "row_A" ), ] ## 注意取的顺序
```
## matrix manipulation, cont.
### 取一列、多列
\FontSmall
```{r}
m[ , 1 ];
m[, c(1,2)];
m[, c("B","A")];## 注意取的顺序
```
## matrix manipulation, cont.
### 取部分
\FontSmall
```{r}
m;
m[1:2, 2:3]; ## 取其中一部分
```
## matrix manipulation, cont.
### 替换一行
\FontSmall
```{r}
m;
m[1, ] <- c(10);
```
## matrix manipulation, cont.
### 替换一列
\FontSmall
```{r}
m[, "C"] <- c(230, 140);
m;
```
## matrix manipulation, cont.
### 替换多行、多列
\FontSmall
```{r, eval=FALSE}
## -- 替换前两行
m[ 1:2 , ] <- matrix( 1:6, nrow = 2 );
## -- 替换其中两列
m[ , c("C", "B") ] <- matrix( 100:103, nrow = 2 );
```
### 替换 subset
\FontSmall
```{r, eval=FALSE}
m[ 1 , c("C", "B") ] <- matrix( 110:111, nrow = 1 );
```
### 转置
\FontSmall
```{r}
t(m);
```
# section 3: 特别值
## 数据类型汇总
![R的数据类型及相互关系](images/talk02/data-structures-overview.png){height=60%}
图片出自:https://r4ds.had.co.nz/vectors.html
## 用 `typeof()` 函数确定以上类型
\FontSmall
```{r}
typeof(letters);
typeof(1:10);
typeof( list("a", "b", 1:10) );
typeof( 1:10 %% 3 == 0 );
typeof(1);
typeof(1L);
```
## NULL
**NULL**: expressions and functions whose value is undefined
\FontSmall
```{r}
typeof( NULL );
```
## 特别值以及它们是如何产生的
一次认识所有:
\FontSmall
```{r}
c(-1, 0, 1) / 0;
```
`NA `
\FontNormal
**注意** NULL 既是特别值也是特别数据类型
## 判定特别值
以下判定为 TRUE:
\FontSmall
```{r warning=FALSE, message=FALSE}
nul <- readr::read_csv(file = "data/talk02/null.csv");
nul;
```
## 其它 is. 函数
\FontSmall
```{r eval=FALSE}
is.null( NULL );
is.numeric( NA );
is.numeric( Inf );
## 用于替代 typeof 的函数
is.list();
is.logical();
is.character();
is.vector();
## 更多 ...
```
# section 4: 简单回顾
## 今日内容回顾
### R基本数据类型
* 数字和字符串
* 整数、小数、逻辑
* 数据类型之间转换;自动规则
* `matrix`
### R数据类型的查看方式
* `mode`
* `class`
* `typeof`
* `is.xxx函数们`
### 基本数据类型的操作
* 定义
* 取部分
* 替换
* 其它
# section 5: 练习 & 作业
## 练习 & 作业
- `Exercises and homework` 目录下 `talk02-homework.Rmd` 文件
- 完成时间:见钉群的要求