多元统计分析及R语言建模#学习笔记

本文介绍了如何在R语言中使用c()函数创建向量,检测数据类型,生成等差数列,操作矩阵(包括加减乘除、转置、维度调整),进行矩阵运算如求逆、特征值与向量分析、奇异值分解等。同时涵盖了数据框的构造、操作以及矩阵的统计汇总。
摘要由CSDN通过智能技术生成

最近在学习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. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
b=c(1,3,6:4,9)
  1. 1
  2. 3
  3. 6
  4. 5
  5. 4
  6. 9

创建矩阵

A=matrix(c(1,4,2,5,3,6),nrow=2,ncol=3)
A
A matrix: 2 × 3 of type dbl
123
456
B=matrix(c(1,2,3,4,5,6),3,2) #3行2列
B
A matrix: 3 × 2 of type dbl
14
25
36
matrix(x1,nrow=3,ncol=4) #按列摆放顺序
A matrix: 3 × 4 of type dbl
171155154166
175152164159
159158168164
matrix(x1,nrow=4,ncol=3)
A matrix: 4 × 3 of type dbl
171152168
175158166
159154159
155164164
matrix(x1,nrow=4,ncol=3,byrow=T) #按行排列的矩阵
A matrix: 4 × 3 of type dbl
171175159
155152158
154164168
166159164

矩阵转置

t(A) #A'为转置矩阵,矩阵A并未发生改变
A matrix: 3 × 2 of type dbl
14
25
36

矩阵加减

A[,1:2]+B[1:2,]
A matrix: 2 × 2 of type dbl
26
610
A[,2:3]-B[2:3,]
A matrix: 2 × 2 of type dbl
0-2
20

矩阵相乘

C=A%*%B
C
A matrix: 2 × 2 of type dbl
1432
3277
D=B%*%A
D
A matrix: 3 × 3 of type dbl
172227
222936
273645

矩阵对角运算

diag(D) #取方阵的对角元素
  1. 17
  2. 29
  3. 45
diag(C)
  1. 14
  2. 77
I=diag(3) #对一个正整数k应用diag()函数将产生k维单位矩阵
I
A matrix: 3 × 3 of type dbl
100
010
001
c=c(1,2,3,4) #对一个向量用diag()函数将产生以这个向量为对角元素的对角矩阵
M=diag(c)
M
A matrix: 4 × 4 of type dbl
1000
0200
0030
0004

矩阵求逆

solve(C)
A matrix: 2 × 2 of type dbl
1.4259259-0.5925926
-0.59259260.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的特征值组成的对角矩阵
A matrix: 3 × 3 of type dbl
172227
222936
273645

矩阵的奇异值分解

(A=matrix(1:18,3,6))
A matrix: 3 × 6 of type int
147101316
258111417
369121518
(A.s=svd(A)) #矩阵的奇异值分解

$d

  1. 45.8945322027251
  2. 1.6407053035306
  3. 1.36652174299492e-15

$u

A matrix: 3 × 3 of type dbl
-0.52903540.743945510.4082483
-0.57607150.03840487-0.8164966
-0.6231077-0.667135770.4082483

$v

A matrix: 6 × 3 of type dbl
-0.07736219-0.71960032-0.4076688
-0.19033085-0.508932470.5745647
-0.30329950-0.29826463-0.0280114
-0.41626816-0.087596790.2226621
-0.529236820.12307105-0.6212052
-0.642205480.333738890.2596585
A.s$u%*%diag(A.s$d)%*%t(A.s$v) #'$'表示从一个dataframe取出某一列数据
A matrix: 3 × 6 of type dbl
147101316
258111417
369121518

矩阵的维数

A=matrix(1:16,4,4)
dim(A)
  1. 4
  2. 4
nrow(A)

4

ncol(A)

4

矩阵的和

A
sum(A)
A matrix: 4 × 4 of type int
15913
261014
371115
481216

136

rowSums(A) #矩阵按行求和
  1. 28
  2. 32
  3. 36
  4. 40
colSums(A) #矩阵按列求和
  1. 10
  2. 26
  3. 42
  4. 58

矩阵的均值

mean(A)
rowMeans(A) #按行求均值
colMeans(A) #按列求均值

8.5

  1. 7
  2. 8
  3. 9
  4. 10
  1. 2.5
  2. 6.5
  3. 10.5
  4. 14.5

数据框的构成

X=data.frame(x1,x2) #x1,x2的长度必须一致
X
A data.frame: 12 × 2
x1x2
<dbl><dbl>
17157
17564
15941
15538
15235
15844
15441
16451
16857
16649
15947
16446
Y=data.frame('身高'=x1,'体重'=x2)
Y
A data.frame: 12 × 2
身高体重
<dbl><dbl>
17157
17564
15941
15538
15235
15844
15441
16451
16857
16649
15947
16446

数据框的组成

rbind(x1,x2) #按行合并
A matrix: 2 × 12 of type dbl
x1171175159155152158154164168166159164
x2576441383544415157494746
cbind(x1,x2) #按列合并
A matrix: 12 × 2 of type dbl
x1x2
17157
17564
15941
15538
15235
15844
15441
16451
16857
16649
15947
16446

按行显示

head(X) #前6行
A data.frame: 6 × 2
x1x2
<dbl><dbl>
117157
217564
315941
415538
515235
615844
tail(X) #后6行
A data.frame: 6 × 2
x1x2
<dbl><dbl>
715441
816451
916857
1016649
1115947
1216446

数据框的应用

apply(x,margin,fun)

x:数据框或矩阵

margin:指定对行还是对列运算(margin=1表示对行运算,margin=2表示对列运算)

fun:指定运算函数

Xr=apply(X,1,sum) #按行求和=rowSums
Xr
  1. 228
  2. 239
  3. 200
  4. 193
  5. 187
  6. 202
  7. 195
  8. 215
  9. 225
  10. 215
  11. 206
  12. 210
Xc=apply(X,2,sum) #按列求和=colSums
Xc

x1 1945

x2 570

cbind(X,'行合计'=Xr) 
A data.frame: 12 × 3
x1x2行合计
<dbl><dbl><dbl>
17157228
17564239
15941200
15538193
15235187
15844202
15441195
16451215
16857225
16649215
15947206
16446210
rbind(X,'列合计'=Xc)
A data.frame: 13 × 2
x1x2
<dbl><dbl>
17157
17564
15941
15538
15235
15844
15441
16451
16857
16649
15947
16446
1945570
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值