最近在学习R语言,把书上的代码都敲一遍,仅供学习
函数c()创建向量
x1=c(171,175,159,155,152,158,154,164,168,166,159,164)
x2=c(57,64,41,38,35,44,41,51,57,49,47,46)
length(x1)
length(x2)
12 12
查看数据类型
mode(x1)
'numeric'
生成等差数列向量
a=1:12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
b=c(1,3,6:4,9)
- 1
- 3
- 6
- 5
- 4
- 9
创建矩阵
A=matrix(c(1,4,2,5,3,6),nrow=2,ncol=3)
A
1 | 2 | 3 |
4 | 5 | 6 |
B=matrix(c(1,2,3,4,5,6),3,2) #3行2列
B
1 | 4 |
2 | 5 |
3 | 6 |
matrix(x1,nrow=3,ncol=4) #按列摆放顺序
171 | 155 | 154 | 166 |
175 | 152 | 164 | 159 |
159 | 158 | 168 | 164 |
matrix(x1,nrow=4,ncol=3)
171 | 152 | 168 |
175 | 158 | 166 |
159 | 154 | 159 |
155 | 164 | 164 |
matrix(x1,nrow=4,ncol=3,byrow=T) #按行排列的矩阵
171 | 175 | 159 |
155 | 152 | 158 |
154 | 164 | 168 |
166 | 159 | 164 |
矩阵转置
t(A) #A'为转置矩阵,矩阵A并未发生改变
1 | 4 |
2 | 5 |
3 | 6 |
矩阵加减
A[,1:2]+B[1:2,]
2 | 6 |
6 | 10 |
A[,2:3]-B[2:3,]
0 | -2 |
2 | 0 |
矩阵相乘
C=A%*%B
C
14 | 32 |
32 | 77 |
D=B%*%A
D
17 | 22 | 27 |
22 | 29 | 36 |
27 | 36 | 45 |
矩阵对角运算
diag(D) #取方阵的对角元素
- 17
- 29
- 45
diag(C)
- 14
- 77
I=diag(3) #对一个正整数k应用diag()函数将产生k维单位矩阵
I
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 1 |
c=c(1,2,3,4) #对一个向量用diag()函数将产生以这个向量为对角元素的对角矩阵
M=diag(c)
M
1 | 0 | 0 | 0 |
0 | 2 | 0 | 0 |
0 | 0 | 3 | 0 |
0 | 0 | 0 | 4 |
矩阵求逆
solve(C)
1.4259259 | -0.5925926 |
-0.5925926 | 0.2592593 |
矩阵的特征值与向量
D.e=eigen(D,symmetric=T)
D.e
eigen() decomposition $values [1] 9.040267e+01 5.973275e-01 1.329470e-15 $vectors [,1] [,2] [,3] [1,] -0.4286671 0.8059639 0.4082483 [2,] -0.5663069 0.1123824 -0.8164966 [3,] -0.7039467 -0.5811991 0.4082483
D.e$vectors%*%diag(D.e$values)%*%t(D.e$vectors) #D=URU',R为D的特征值组成的对角矩阵
17 | 22 | 27 |
22 | 29 | 36 |
27 | 36 | 45 |
矩阵的奇异值分解
(A=matrix(1:18,3,6))
1 | 4 | 7 | 10 | 13 | 16 |
2 | 5 | 8 | 11 | 14 | 17 |
3 | 6 | 9 | 12 | 15 | 18 |
(A.s=svd(A)) #矩阵的奇异值分解
$d
- 45.8945322027251
- 1.6407053035306
- 1.36652174299492e-15
$u
-0.5290354 | 0.74394551 | 0.4082483 |
-0.5760715 | 0.03840487 | -0.8164966 |
-0.6231077 | -0.66713577 | 0.4082483 |
$v
-0.07736219 | -0.71960032 | -0.4076688 |
-0.19033085 | -0.50893247 | 0.5745647 |
-0.30329950 | -0.29826463 | -0.0280114 |
-0.41626816 | -0.08759679 | 0.2226621 |
-0.52923682 | 0.12307105 | -0.6212052 |
-0.64220548 | 0.33373889 | 0.2596585 |
A.s$u%*%diag(A.s$d)%*%t(A.s$v) #'$'表示从一个dataframe取出某一列数据
1 | 4 | 7 | 10 | 13 | 16 |
2 | 5 | 8 | 11 | 14 | 17 |
3 | 6 | 9 | 12 | 15 | 18 |
矩阵的维数
A=matrix(1:16,4,4)
dim(A)
- 4
- 4
nrow(A)
4
ncol(A)
4
矩阵的和
A
sum(A)
1 | 5 | 9 | 13 |
2 | 6 | 10 | 14 |
3 | 7 | 11 | 15 |
4 | 8 | 12 | 16 |
136
rowSums(A) #矩阵按行求和
- 28
- 32
- 36
- 40
colSums(A) #矩阵按列求和
- 10
- 26
- 42
- 58
矩阵的均值
mean(A)
rowMeans(A) #按行求均值
colMeans(A) #按列求均值
8.5
- 7
- 8
- 9
- 10
- 2.5
- 6.5
- 10.5
- 14.5
数据框的构成
X=data.frame(x1,x2) #x1,x2的长度必须一致
X
x1 | x2 |
---|---|
<dbl> | <dbl> |
171 | 57 |
175 | 64 |
159 | 41 |
155 | 38 |
152 | 35 |
158 | 44 |
154 | 41 |
164 | 51 |
168 | 57 |
166 | 49 |
159 | 47 |
164 | 46 |
Y=data.frame('身高'=x1,'体重'=x2)
Y
身高 | 体重 |
---|---|
<dbl> | <dbl> |
171 | 57 |
175 | 64 |
159 | 41 |
155 | 38 |
152 | 35 |
158 | 44 |
154 | 41 |
164 | 51 |
168 | 57 |
166 | 49 |
159 | 47 |
164 | 46 |
数据框的组成
rbind(x1,x2) #按行合并
x1 | 171 | 175 | 159 | 155 | 152 | 158 | 154 | 164 | 168 | 166 | 159 | 164 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
x2 | 57 | 64 | 41 | 38 | 35 | 44 | 41 | 51 | 57 | 49 | 47 | 46 |
cbind(x1,x2) #按列合并
x1 | x2 |
---|---|
171 | 57 |
175 | 64 |
159 | 41 |
155 | 38 |
152 | 35 |
158 | 44 |
154 | 41 |
164 | 51 |
168 | 57 |
166 | 49 |
159 | 47 |
164 | 46 |
按行显示
head(X) #前6行
x1 | x2 | |
---|---|---|
<dbl> | <dbl> | |
1 | 171 | 57 |
2 | 175 | 64 |
3 | 159 | 41 |
4 | 155 | 38 |
5 | 152 | 35 |
6 | 158 | 44 |
tail(X) #后6行
x1 | x2 | |
---|---|---|
<dbl> | <dbl> | |
7 | 154 | 41 |
8 | 164 | 51 |
9 | 168 | 57 |
10 | 166 | 49 |
11 | 159 | 47 |
12 | 164 | 46 |
数据框的应用
apply(x,margin,fun)
x:数据框或矩阵
margin:指定对行还是对列运算(margin=1表示对行运算,margin=2表示对列运算)
fun:指定运算函数
Xr=apply(X,1,sum) #按行求和=rowSums
Xr
- 228
- 239
- 200
- 193
- 187
- 202
- 195
- 215
- 225
- 215
- 206
- 210
Xc=apply(X,2,sum) #按列求和=colSums
Xc
x1 1945
x2 570
cbind(X,'行合计'=Xr)
x1 | x2 | 行合计 |
---|---|---|
<dbl> | <dbl> | <dbl> |
171 | 57 | 228 |
175 | 64 | 239 |
159 | 41 | 200 |
155 | 38 | 193 |
152 | 35 | 187 |
158 | 44 | 202 |
154 | 41 | 195 |
164 | 51 | 215 |
168 | 57 | 225 |
166 | 49 | 215 |
159 | 47 | 206 |
164 | 46 | 210 |
rbind(X,'列合计'=Xc)
x1 | x2 |
---|---|
<dbl> | <dbl> |
171 | 57 |
175 | 64 |
159 | 41 |
155 | 38 |
152 | 35 |
158 | 44 |
154 | 41 |
164 | 51 |
168 | 57 |
166 | 49 |
159 | 47 |
164 | 46 |
1945 | 570 |