矩阵matrix
数据框dataframe:可以把不同类型的数据放到一起
> x1=c(171,175,159,155,152,158,154,164,168,166,159,164)
> x1
[1] 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)
> x2
[1] 57 64 41 38 35 44 41 51 57 49 47 46
> rbind(x1,x2)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
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
[1,] 171 57
[2,] 175 64
[3,] 159 41
[4,] 155 38
[5,] 152 35
[6,] 158 44
[7,] 154 41
[8,] 164 51
[9,] 168 57
[10,] 166 49
[11,] 159 47
[12,] 164 46
> matrix(x1,nrow = 3,ncol = 4)
[,1] [,2] [,3] [,4]
[1,] 171 155 154 166
[2,] 175 152 164 159
[3,] 159 158 168 164
> matrix(x1,nrow = 4,ncol = 3)
[,1] [,2] [,3]
[1,] 171 152 168
[2,] 175 158 166
[3,] 159 154 159
[4,] 155 164 164
> A=B=matrix(1:12,nrow = 3,ncol = 4)
> A+B
[,1] [,2] [,3] [,4]
[1,] 2 8 14 20
[2,] 4 10 16 22
[3,] 6 12 18 24
> A-B
[,1] [,2] [,3] [,4]
[1,] 0 0 0 0
[2,] 0 0 0 0
[3,] 0 0 0 0
> A*B
[,1] [,2] [,3] [,4]
[1,] 1 16 49 100
[2,] 4 25 64 121
[3,] 9 36 81 144
> A%*%B
Error in A %*% B : non-conformable arguments
> A%*%B
Error in A %*% B : non-conformable arguments
> A=matrix(1:12,nrow = 4,ncol = 4)
> A
[,1] [,2] [,3] [,4]
[1,] 1 5 9 1
[2,] 2 6 10 2
[3,] 3 7 11 3
[4,] 4 8 12 4
> diag(A)
[1] 1 6 11 4
> diag(diag(A))
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 6 0 0
[3,] 0 0 11 0
[4,] 0 0 0 4
> A=matrix(rnorm(16), nrow = 4, ncol = 4)
> A
[,1] [,2] [,3] [,4]
[1,] 0.97993343 -0.1133519 -0.7210848 1.3739694
[2,] -0.46153680 -0.1212796 -0.5967456 0.9334681
[3,] 0.07862929 1.0272289 -1.4153027 0.6902507
[4,] 0.05170289 1.1535131 0.5469108 -0.6584536
> solve(A)
[,1] [,2] [,3] [,4]
[1,] 0.5644873 -0.9679524 0.07635672 -0.1142969
[2,] 0.1022195 0.3276392 0.12050088 0.8041007
[3,] 0.3605646 0.7175637 -0.85555813 0.8727691
[4,] 0.5228824 1.0939776 -0.49353026 0.6059016
> diag(4)
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 1 0 0
[3,] 0 0 1 0
[4,] 0 0 0 1
> diag(4)+1
[,1] [,2] [,3] [,4]
[1,] 2 1 1 1
[2,] 1 2 1 1
[3,] 1 1 2 1
[4,] 1 1 1 2
> A.e=eigen(A,symmetric = T)
> A.e
eigen() decomposition
$values
[1] 1.462951 0.850270 -1.502006 -2.026318
$vectors
[,1] [,2] [,3] [,4]
[1,] 0.5380490 0.8323777 -0.07649193 -0.1086260
[2,] -0.6622061 0.3198984 -0.39197856 -0.5527214
[3,] -0.3023784 0.2628639 -0.42165014 0.8134378
[4,] -0.4249160 0.3683951 0.81407183 0.1449776
> apply(A, 1, mean)
[1] 0.37986651 -0.06152345 0.09520154 0.27341830
> apply(A, 1, sum)
[1] 1.5194660 -0.2460938 0.3808062 1.0936732
> apply(A, 1, rowMeans)
Error in FUN(newX[, i], ...) : 'x'必需是阵列,而且至少得有两个维度
> apply(A, 1, rowMean)
Error in match.fun(FUN) : object 'rowMean' not found
> rnorm(100)
[1] 1.663888700 0.484898176 0.762900312 1.004235239 -0.259142028 -0.396690350
[7] -1.962667180 -0.666637195 0.909089023 0.472630016 -0.900359228 -1.609149660
[13] -0.471906140 0.991698563 0.790954359 -0.192902101 -0.005083808 1.773716603
[19] -0.004060147 -1.211412795 -1.563126569 0.775584202 -0.526888116 1.464635177
[25] 0.837500891 0.272296759 -0.310935144 -0.136937126 -0.034447814 0.199286210
[31] -0.434172084 0.003831882 -0.538692873 1.108858808 -0.183996053 -0.361898943
[37] 1.388276291 -1.041460362 0.085138695 0.541204548 0.202810817 0.289542258
[43] 1.533320186 -0.235446934 -0.100609415 0.036397605 1.275502026 -0.669806976
[49] -1.330488847 2.061561689 -0.865050869 -0.970927679 1.568164139 -0.104257372
[55] 1.185284963 -0.530891191 -0.456649101 2.314369738 -0.996830151 -1.550247278
[61] -0.177499056 1.093706241 -1.070930386 -0.162603254 -0.725918885 0.366013698
[67] -0.138413484 0.423542694 1.527400959 -1.152445112 -1.150058826 1.090705377
[73] 1.934869138 1.335123701 -0.627019335 0.939448113 0.990455800 -1.724482773
[79] 0.414340631 -0.569025538 -0.409252170 0.599669366 -0.270184294 1.112363473
[85] -0.038782900 0.122503720 -0.008521498 0.901640099 0.647379284 -0.006002374
[91] 0.928638411 -0.439443436 -0.253073407 -0.931795977 0.187267011 0.084665737
[97] -0.243929644 -0.448317924 -0.625875661 0.450916788
> matrix(rnorm(100),20,5)
[,1] [,2] [,3] [,4] [,5]
[1,] 0.701547238 1.68470190 0.23485135 0.46761559 -1.41602944
[2,] 0.260206103 -1.86518130 0.64235895 1.74300763 0.12038661
[3,] -0.045774851 2.25735978 1.91843000 1.12320985 -0.27952360
[4,] 1.163878283 0.31698219 0.24303724 1.85208562 1.38436171
[5,] 0.441172768 -0.42341407 1.06970259 -0.80638188 0.13687507
[6,] -0.501595720 -0.95345162 -1.17295481 1.26058932 -0.86145842
[7,] -0.122349286 -0.81832182 0.19265376 1.18685199 -0.72764598
[8,] -0.611758301 -0.31503748 0.30753510 0.53488766 -1.27254986
[9,] -0.004159843 0.15395966 -2.26882212 0.02751649 -0.34386051
[10,] -1.317815437 1.93805926 -1.09534599 2.04376173 -0.36138093
[11,] 1.263569990 0.99987484 0.99088936 -1.84485673 -0.65071849
[12,] 0.011597197 -0.84463202 0.03640103 -0.64712107 0.34132928
[13,] -1.262812110 -0.85385606 0.41914362 0.59595644 -0.42540130
[14,] 0.362775112 0.74967377 0.50108911 -0.90191321 -0.55203729
[15,] -0.492185834 -1.01464029 -0.09594038 0.19179107 -0.83759598
[16,] 0.865633806 -0.09588912 0.19268708 -0.61561724 1.09389453
[17,] -0.872007350 -0.22816583 0.73432952 -0.64910042 -0.56606234
[18,] -0.339897887 -1.24744447 -0.97225305 0.02801266 0.06986353
[19,] 2.099933382 0.63889697 -0.90587777 0.31812770 -0.24971995
[20,] -0.394631570 1.75099283 -0.69655798 0.27789265 -0.20589554
> A=matrix(rnorm(100),20,5)
> apply(A, 2, var)
[1] 0.9759652 0.7610062 1.2894139 1.4763183 0.6422701
>
>
> X=data.frame(x1,x2)
> X
x1 x2
1 171 57
2 175 64
3 159 41
4 155 38
5 152 35
6 158 44
7 154 41
8 164 51
9 168 57
10 166 49
11 159 47
12 164 46
> X=data.frame('身高'=x1,'体重'=x2)
> X
身高 体重
1 171 57
2 175 64
3 159 41
4 155 38
5 152 35
6 158 44
7 154 41
8 164 51
9 168 57
10 166 49
11 159 47
12 164 46
> dat=read.table("clipboard",header = TURE)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") : clipboard cannot be opened or contains no text
> dat=read.table("clipboard",header = TURE)
Error in read.table("clipboard", header = TURE) : object 'TURE' not found
> dat=read.table("clipboard",header = T)
> d2.1=dat
> d
Error: object 'd' not found
>
> head(d2.1)
年龄 性别 风险 专兼职 职业 教育 结果
1 20-29 男 有 兼职 金融 高中 赚钱
2 50-59 女 有 兼职 科教 中学 持平
3 40-49 女 无 专职 科教 中学 赔钱
4 30-39 男 有 兼职 工人 中专 赚钱
5 50-59 女 有 专职 农民 大专 赚钱
6 40-49 女 有 兼职 管理 小学 赚钱
> attach(d2.1)
> table(年龄)
年龄
* 0-19 20-29 30-39 40-49 50-59 60-
20 3 92 167 157 51 24
> barplot(table(年龄),col = 1:7)
> pie(table(结果))
> barplot((ta))
Error in barplot((ta)) : object 'ta' not found
> barplot((table(年龄,性别)))
> barplot(table(年龄,性别),beside = T,col = 1:7)
> barplot(table(年龄,性别),beside = T,col = 1:2)
> ftable(年龄,性别,结果)
结果 持平 赔钱 赚钱
年龄 性别
* 男 4 3 2
女 3 7 1
0-19 男 0 0 2
女 1 0 0
20-29 男 21 17 31
女 10 7 6
30-39 男 31 30 40
女 30 20 16
40-49 男 31 30 28
女 25 30 13
50-59 男 5 11 8
女 8 10 9
60- 男 7 5 3
女 2 5 2
> detach(d2.1)
参考资料: https://next.xuetangx.com/course/JNU07011000851/1515699