第1章 统计数据的表示
- 生成一个向量 x=(2,3,1,4,6,1,7,8,10),并按该向量构造一个3阶方阵A,其中矩阵的第1列为该向量的前3个元素,第2列为该向量的第45、6个元素,第3列为该向量的第7、8、9个元素,求出矩阵A的逆矩阵,并解线性方程组
># 构造A;
> a <- c(2,3,1,4,6,1,7,8,10)
> A <- matrix(a,3)
> A
[,1] [,2] [,3]
[1,] 2 4 7
[2,] 3 6 8
[3,] 1 1 10
>
# 求A的逆矩阵
> solve(A)
[,1] [,2] [,3]
[1,] -10.4 6.6 2
[2,] 4.4 -2.6 -1
[3,] 0.6 -0.4 0
>
# 解线性方程组;
> B <- c(11,21,43)
> Y <- solve(A,B)
> y1 = Y[1];y1
> y2 = Y[2];y2
> y3 = Y[3];y3
- 构造一个10阶方阵,使得方阵中的元素全部为1/5,并验证这个矩阵的秩为1。(提示:可使用kronecker()函数。)
># 输入A;
> A = (diag(10)-diag(10))+0.2
> A
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
[2,] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
[3,] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
[4,] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
[5,] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
[6,] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
[7,] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
[8,] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
[9,] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
[10,] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
># 验证矩阵的秩;
> qr(A)$rank
[1] 1
- 求出矩阵
的特征根与特征值,并对其进行QR分解,求出每一行及每一列的元素之和、平均值。
># 输入A;
> a <- c(11,2,3,7,5,12,2,5,3,1,9,11,12,10,7,21,23,15,5,26,31,24,3,7,8,9,21,3,15,17,21,25,3,7,9,11)
> A <- matrix(a,6)
> A <- t(A)
> A
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 11 2 3 7 5 12
[2,] 2 5 3 1 9 11
[3,] 12 10 7 21 23 15
[4,] 5 26 31 24 3 7
[5,] 8 9 21 3 15 17
[6,] 21 25 3 7 9 11
># 特征值与特征向量;
> eigen(A)$values #特征值
[1] 66.439200+0.000000i -19.609176+0.000000i 19.559389+0.000000i -11.340282+0.000000i 8.975435+2.826264i 8.975435-2.826264i
> eigen(A)$vectors #特征向量
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -0.2195050+0i 0.09508503+0i 0.02593203+0i 0.36958937+0i 0.5556389+0.0000000i 0.5556389+0.0000000i
[2,] -0.1637092+0i 0.06318164+0i -0.31292635+0i 0.32133706+0i -0.4972531+0.0283257i -0.4972531-0.0283257i
[3,] -0.5297450+0i 0.70454393+0i 0.24765681+0i -0.03172883+0i 0.1946232-0.1092901i 0.1946232+0.1092901i
[4,] -0.5973865+0i -0.52919204+0i 0.80673363+0i -0.13152065+0i 0.3548740+0.2381760i 0.3548740-0.2381760i
[5,] -0.4228551+0i -0.45370034+0i -0.24388331+0i 0.33133572+0i -0.2660377-0.3085823i -0.2660377+0.3085823i
[6,] -0.3297120+0i 0.06853137+0i -0.36023952+0i -0.79502103+0i -0.1556850+0.1431067i -0.1556850-0.1431067i
># QR分解;
> qr.Q(qr(A))
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -0.38915203 0.433096940 -0.32325300 -0.1414950 -0.56490811 -0.46621157
[2,] -0.07075491 -0.120039382 0.06179501 0.1090202 0.56004246 -0.80698991
[3,] -0.42452948 0.137264524 -0.13072068 -0.7189957 0.46233402 0.23051609
[4,] -0.17688728 -0.878911507 -0.23343899 -0.2321290 -0.27997049 -0.09728477
[5,] -0.28301966 -0.008532079 -0.70875266 0.5407033 0.26802632 0.23086475
[6,] -0.74292659 -0.081349890 0.56371702 0.3238749 -0.05706962 0.12455332
> qr.R(qr(A))
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -28.26659 -31.09678 -18.00713 -22.004778 -23.8090285 -26.0378083
[2,] 0.00000 -23.32359 -25.76946 -15.894727 0.7453496 -1.2565768
[3,] 0.00000 0.00000 -22.12868 -8.728885 -10.3248393 -12.6420821
[4,] 0.00000 0.00000 0.00000 -17.662218 -5.9341587 -0.1539773
[5,] 0.00000 0.00000 0.00000 0.000000 15.5163808 8.2854682
[6,] 0.00000 0.00000 0.00000 0.000000 0.0000000 -6.3998925
># 行列之和与平均
> rowSums(A)#行和
[1] 40 31 88 96 73 76
> rowMeans(A)#行平均
[1] 6.666667 5.166667 14.666667 16.000000 12.166667 12.666667
> colSums(A)#列和
[1] 59 77 68 63 64 73
> colMeans(A)#列平均
[1] 9.833333 12.833333 11.333333 10.500000 10.666667 12.166667
- 在Excel文件中输入班级中若干同学 的身高及体重值,然后将其导入 R 中.
> install.packages("readxl")
将程序包安装入‘E:/文档/R/win-library/4.1’
(因为‘lib’没有被指定)
试开URL’https://mirrors.e-ducation.cn/CRAN/bin/windows/contrib/4.1/readxl_1.4.0.zip'
Content type 'application/zip' length 1662853 bytes (1.6 MB)
downloaded 1.6 MB
package ‘readxl’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\admin\AppData\Local\Temp\RtmpYdqYL5\downloaded_packages
> library(readxl)
Warning message:
程辑包‘readxl’是用R版本4.1.3 来建造的
> data4 <- read_excel("E:/example4.xlsx")
-/ / - > data4
# A tibble: 4 x 3
name height weight
<chr> <dbl> <dbl>
1 a 167 120
2 b 150 76
3 c 161 90
4 d 178 140
-end-
ps:
关注公众号“数理统计科代表”
更新更快,也有更多习题答案哦~