-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathR_basicfun.Rmd
221 lines (146 loc) · 2.56 KB
/
R_basicfun.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
---
title: "R的常用函数与数据结构"
author: "李峰"
date: "2019年4月"
output:
html_document:
number_sections: yes
theme: lumen
toc: yes
toc_depth: 5
---
---
# 基础函数
## 首先要学习的函数
+ ?
R中的帮助功能
```{r}
? str
```
+ str
显示对象内部结构
```{r}
x <- 1:7
str(x)
```
## 重要的操作符
+ %in%, match
+ =, <-, <<-
+ $, [, [[, head, tail, subset
+ with
+ assign, get
## 比较
+ all.equal, identical
+ !=, ==, >, >=, <, <=
+ is.na, complete.cases
+ is.finite
## 基础数学
+ *, +, -, /, ^, %%, %/%
+ abs, sign
+ acos, asin, atan, atan2
+ sin, cos, tan
+ ceiling, floor, round, trunc, signif
+ exp, log, log10, log2, sqrt
+ max, min, prod, sum
+ cummax, cummin, cumprod, cumsum, diff
+ pmax, pmin
+ range
+ mean, median, cor, sd, var
+ rle
## 处理函数的函数
+ function
+ missing
+ on.exit
+ return, invisible
## 逻辑与集合
+ &, |, !, xor
+ all, any
+ intersect, union, setdiff, setequal
+ which
## 向量和矩阵
+ c, matrix
+ length, dim, ncol, nrow
+ cbind, rbind
+ names, colnames, rownames
+ t
+ diag
+ sweep
+ as.matrix, data.matrix
## 构建向量
+ c
+ rep, rep_len
+ seq, seq_len, seq_along
+ rev
+ sample
+ choose, factorial, combn
+ (is/as).(character/numeric/logical/...)
## 列表和数据框
+ list, unlist
+ data.frame, as.data.frame
+ split
+ expand.grid
## 控制流
+ if, &&, || (short circuiting)
+ for, while
+ next, break
+ switch
+ ifelse
## apply函数
+ lapply, sapply, vapply
+ apply
+ tapply
+ replicate
# 常见数据结构
## 日期数据
+ ISOdate, ISOdatetime, strftime, strptime, date
+ difftime
+ julian, months, quarters, weekdays
+ library(lubridate)
## 字符处理
+ grep, agrep
+ gsub
+ strsplit
+ chartr
+ nchar
+ tolower, toupper
+ substr
+ paste
+ trimws
+ library(stringr)
## 因子
+ factor, levels, nlevels
+ reorder, relevel
+ cut, findInterval
+ interaction
+ options(stringsAsFactors = FALSE)
## 数组处理
+ array
+ dim
+ dimnames
+ aperm
+ library(abind)
# 统计函数
## 排序和制表
+ duplicated, unique
+ merge
+ order, rank, quantile
+ sort
+ table, ftable
## 线性模型
+ fitted, predict, resid, rstandard
+ lm, glm
+ hat, influence.measures
+ logLik, df, deviance
+ formula, ~, I
+ anova, coef, confint, vcov
+ contrasts
## 随机变量
+ (q, p, d, r) * (beta, binom, cauchy, chisq, exp, f, gamma, geom,
hyper, lnorm, logis, multinom, nbinom, norm, pois, signrank, t,
unif, weibull, wilcox, birthday, tukey)
## 矩阵算法
+ crossprod, tcrossprod
+ eigen, qr, svd
+ %*%, %o%, outer
+ rcond
+ solve