-
创建向量矩阵
> x1=c(2,3,6,8) > x2=c(1,2,3,4) > a1=(1:100) > length(a1) [1] 100 > length(x1) [1] 4 > mode(x1) [1] "numeric" > rbind(x1,x2) [,1] [,2] [,3] [,4] x1 2 3 6 8 x2 1 2 3 4 > cbind(x1,x2) x1 x2 [1,] 2 1 [2,] 3 2 [3,] 6 3 [4,] 8 4
-
求平均值,和,连乘,最值,方差,标准差
> mean(x1) [1] 4.75 > sum(x1) [1] 19 > max(x1) [1] 8 > min(x1) [1] 2 > var(x1) [1] 7.583333 > prod(x1) [1] 288 > sd(x1) [1] 2.753785
-
产生向量
> 1:10 [1] 1 2 3 4 5 6 7 8 9 10 > 1:10-1 [1] 0 1 2 3 4 5 6 7 8 9 > 1:10*2 [1] 2 4 6 8 10 12 14 16 18 20 > a=2:60*2+1 > a [1] 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 [20] 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 [39] 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 [58] 119 121 > a[5] [1] 13 > a[-5] [1] 5 7 9 11 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 [20] 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 [39] 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 [58] 121 > a[c(2,3,8)] [1] 7 9 19 > a[a<20] [1] 5 7 9 11 13 15 17 19 > a[a[3]] [1] 21 > seq(6,20) [1] 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 > seq(5,121,by=2) [1] 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 [20] 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 [39] 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 [58] 119 121 > seq(5,121,length=10) [1] 5.00000 17.88889 30.77778 43.66667 56.55556 69.44444 82.33333 [8] 95.22222 108.11111 121.00000
-
新建向量
> a=c(2,3,4,2,3,2,1,4,3,2,1) > which.max(a) [1] 3 > a[which.max(a)] [1] 4 > which(a==2) [1] 1 4 6 10 > a[which(a==2)] [1] 2 2 2 2 > which(a>5) integer(0) > a[which(a>5)] numeric(0) > a=1:20 > a [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 > rev(a) [1] 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 > a=c(2,3,4,5,6,6,7,8,3,2) > sort(a) [1] 2 2 3 3 4 5 6 6 7 8 > rev(sort(a)) [1] 8 7 6 6 5 4 3 3 2 2
-
生成矩阵
> a1=c(1:12) > matrix(a1,nrow=3,ncol=4) [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > matrix(a1,nrow=4,ncol=3) [,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12 > matrix(a1,nrow=4,ncol=3,byrow=T) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12
-
矩阵运算
矩阵相加
> a=matrix(1:12,nrow=3,ncol=4) > t(a) [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [4,] 10 11 12 > 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=matrix(1:12,nrow=3,ncol=4) > b=matrix(1:12,nrow=4,ncol=3) > a%*%b [,1] [,2] [,3] [1,] 70 158 246 [2,] 80 184 288 [3,] 90 210 330 > a=matrix(1:16,nrow=4,ncol=4) > a [,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16 > diag(a) [1] 1 6 11 16 > 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 16 > 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
矩阵求逆
> a=matrix(rnorm(16),4,4) > a [,1] [,2] [,3] [,4] [1,] -1.604650746 -2.22482987 1.5094439 1.0070701 [2,] 0.006409861 -0.01506928 -0.6651050 -1.9342548 [3,] -1.606959408 -0.49430092 -0.9376593 0.1979031 [4,] 0.422441416 -0.33201336 0.3848287 1.1256368 > solve(a) [,1] [,2] [,3] [,4] [1,] -0.1426715 0.5944611 -0.1676185 1.1786143 [2,] -0.1804919 -0.9604913 -0.2055298 -1.4528592 [3,] 0.3168603 -0.5776493 -0.6252734 -1.1661647 [4,] -0.1080209 -0.3089139 0.2160497 0.4162172
解线性方程组
> a=matrix(rnorm(16),4,4)> a [,1] [,2] [,3] [,4][1,] -1.604650746 -2.22482987 1.5094439 1.0070701[2,] 0.006409861 -0.01506928 -0.6651050 -1.9342548[3,] -1.606959408 -0.49430092 -0.9376593 0.1979031[4,] 0.422441416 -0.33201336 0.3848287 1.1256368> solve(a) [,1] [,2] [,3] [,4][1,] -0.1426715 0.5944611 -0.1676185 1.1786143[2,] -0.1804919 -0.9604913 -0.2055298 -1.4528592[3,] 0.3168603 -0.5776493 -0.6252734 -1.1661647[4,] -0.1080209 -0.3089139 0.2160497 0.4162172> a=matrix(rnorm(16),4,4)> a [,1] [,2] [,3] [,4][1,] 1.0451867 -0.2426553 -0.51232551 -0.12062549[2,] -1.5518006 -0.1333096 0.03677731 -0.10715366[3,] -1.0620249 -1.3160312 0.01713207 0.09320016[4,] -0.6664664 2.2398778 1.94861889 0.01788447> b=c(1:4)> b[1] 1 2 3 4> solve(a,b)[1] 0.9840158 -4.6924392 8.0064010 -24.3295023
矩阵的特征值与特征向量
> a=diag(4)+1 > a [,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 $values [1] 5 1 1 1 $vectors [,1] [,2] [,3] [,4] [1,] -0.5 0.8660254 0.0000000 0.0000000 [2,] -0.5 -0.2886751 -0.5773503 -0.5773503 [3,] -0.5 -0.2886751 -0.2113249 0.7886751 [4,] -0.5 -0.2886751 0.7886751 -0.2113249 > a.e$vectors%*%diag(a.e$values)%*%t(a.e$vectors) [,1] [,2] [,3] [,4] [1,] 2 1 1 1 [2,] 1 2 1 1 [3,] 1 1 2 1 [4,] 1 1 1 2
-
数据框
> x1=c(10,13,14,23,43) > x2=c(12,35,35,67,54) > x=data.frame(x1,x2) > x x1 x2 1 10 12 2 13 35 3 14 35 4 23 67 5 43 54 > plot(x)#散点图
-
读文本文件
(x=read.table("abc.txt")) #读剪贴板 y=read.table("clipboard",header=F) y z=read.table("clipboard",header=T) z
-
循环语句
for语句
> for(i in 1:59) {a[i]=1*2+3} > a [1] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 [39] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 > > b=0 > for(i in 1:59) {a[i]=i*2+3;b[i]=i*5-4} > b [1] 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 [20] 96 101 106 111 116 121 126 131 136 141 146 151 156 161 166 171 176 181 186 [39] 191 196 201 206 211 216 221 226 231 236 241 246 251 256 261 266 271 276 281 [58] 286 291
while语句
a[1]=5 > i=1 > while(a[i]<121) {i=i+1;a[i]=a[i-1]+2} > a [1] 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 [20] 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 [39] 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 [58] 119 121
-
R脚本引用
source() print()
时间: 2024-11-14 22:07:40