赋值:
x = c(1:100)
x <-c(1:100)
求和:
sum(x)
求最大值:
max(x)
求最小值:
min(x)
求方差:
var(x)
求累积:
prod(x)
求标准差:
sd(x)
帮助信息:
help(name)//name为所要查询的函数
产生向量:
> 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
> 2:60*2+1
[1] 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
[19] 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75
[37] 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111
[55] 113 115 117 119 121
> 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
[19] 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75
[37] 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111
[55] 113 115 117 119 121
向量查询:
查询单个向量值:
> a[5] ##查询第5个向量值
[1] 13
> a[-5] ##不显示第5个向量值
[1] 5 7 9 11 15 17 19 21 23 25 27 29 31 33 35 37 39 41
[19] 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77
[37] 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113
[55] 115 117 119 121
查询多个向量值:
> a[1:5] ##查询第1:5个向量值
[1] 5 7 9 11 13
> a[-(1:5)] ##不显示第1:5个向量值
[1] 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
[19] 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85
[37] 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121
> a[c(2,4,7)] ##查询第2、4、7个向量值
[1] 7 11 17
> a[3:8] ##查询第3到8个向量值
[1] 9 11 13 15 17 19
> a[a<20] ##查询小于20的向量值
[1] 5 7 9 11 13 15 17 19
> a[a>30 & a<50] ##查询大于30并且小于50的向量值
[1] 31 33 35 37 39 41 43 45 47 49
> a[a[3]] ##查询第a[3]个向量值
[1] 21
> seq(1,10) ##生成1:10的连续向量,默认间距为1
[1] 1 2 3 4 5 6 7 8 9 10
> seq(1,19,by=2) ##生成1:19的连续向量,间距为2
[1] 1 3 5 7 9 11 13 15 17 19
> seq(5,121,length=10) ##生成从5到121的向量,长度为10
[1] 5.00000 17.88889 30.77778 43.66667 56.55556 69.44444 82.33333
[8] 95.22222 108.11111 121.00000
产生字母向量:
> letters[1:30]
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r"
[19] "s" "t" "u" "v" "w" "x" "y" "z" NA NA NA NA
which函数:真正的查询函数
> a<-c(2,3,4,2,5,1,6,3,2,5,8,5,7,3)
> which.max(a)
[1] 11
> which.min(a)
[1] 6
> a[which.max(a)]
[1] 8
> which(a==2)
[1] 1 4 9
> which(a>5)
[1] 7 11 13
> a[which(a>5)]
[1] 6 8 7
rev函数,sort函数:
> a<-1:20
> rev(a)
[1] 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
> sort(a)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> sort(rev(a))
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> rev(sort(a))
[1] 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
> a<-c(1:12)
> matrix(a,nrow=3,ncol=4)
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> matrix(a,nrow=4,ncol=3)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> matrix(a,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<-c(1:12)
> matrix(a,nrow=3,ncol=4)
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> matrix(a,nrow=4,ncol=3)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> matrix(a,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
> t(a)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1 2 3 4 5 6 7 8 9 10 11 12
> a
[1] 1 2 3 4 5 6 7 8 9 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
> b=a
> b
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> 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
错误于a %*% b : 非整合参数
> a%*%t(b)
[,1] [,2] [,3]
[1,] 166 188 210
[2,] 188 214 240
[3,] 210 240 270
> diag(a)
[1] 1 5 9
> diag(diag(a))
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 5 0
[3,] 0 0 9
> 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,] 0.2444748 -0.06604418 0.7758382 -1.03614099
[2,] 1.2399238 -0.22550548 -0.9208650 1.32678104
[3,] -0.8085401 -1.48942704 -1.3238675 -1.44913698
[4,] -0.2688544 0.78279487 -0.3427804 -0.04961982
> solve(a)##求逆矩阵
[,1] [,2] [,3] [,4]
[1,] 0.87542310 0.6740288 -0.01689147 0.2358922
[2,] 0.25783626 0.0663873 -0.15839453 1.0169742
[3,] 0.01296783 -0.3604023 -0.31519186 -0.7024465
[4,] -0.76529044 -0.1150571 -0.22989766 -0.5351399
> b<-c(1:4)
> solve(a,b)##解线性方程组
[1] 3.116375 3.983324 -4.463198 -3.825657
矩阵的特征值与特征向量:
> 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
x = c(1:100)
x <-c(1:100)
求和:
sum(x)
求最大值:
max(x)
求最小值:
min(x)
求方差:
var(x)
求累积:
prod(x)
求标准差:
sd(x)
帮助信息:
help(name)//name为所要查询的函数
产生向量:
> 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
> 2:60*2+1
[1] 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39
[19] 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75
[37] 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111
[55] 113 115 117 119 121
> 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
[19] 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75
[37] 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111
[55] 113 115 117 119 121
向量查询:
查询单个向量值:
> a[5] ##查询第5个向量值
[1] 13
> a[-5] ##不显示第5个向量值
[1] 5 7 9 11 15 17 19 21 23 25 27 29 31 33 35 37 39 41
[19] 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77
[37] 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113
[55] 115 117 119 121
查询多个向量值:
> a[1:5] ##查询第1:5个向量值
[1] 5 7 9 11 13
> a[-(1:5)] ##不显示第1:5个向量值
[1] 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
[19] 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85
[37] 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121
> a[c(2,4,7)] ##查询第2、4、7个向量值
[1] 7 11 17
> a[3:8] ##查询第3到8个向量值
[1] 9 11 13 15 17 19
> a[a<20] ##查询小于20的向量值
[1] 5 7 9 11 13 15 17 19
> a[a>30 & a<50] ##查询大于30并且小于50的向量值
[1] 31 33 35 37 39 41 43 45 47 49
> a[a[3]] ##查询第a[3]个向量值
[1] 21
> seq(1,10) ##生成1:10的连续向量,默认间距为1
[1] 1 2 3 4 5 6 7 8 9 10
> seq(1,19,by=2) ##生成1:19的连续向量,间距为2
[1] 1 3 5 7 9 11 13 15 17 19
> seq(5,121,length=10) ##生成从5到121的向量,长度为10
[1] 5.00000 17.88889 30.77778 43.66667 56.55556 69.44444 82.33333
[8] 95.22222 108.11111 121.00000
产生字母向量:
> letters[1:30]
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r"
[19] "s" "t" "u" "v" "w" "x" "y" "z" NA NA NA NA
which函数:真正的查询函数
> a<-c(2,3,4,2,5,1,6,3,2,5,8,5,7,3)
> which.max(a)
[1] 11
> which.min(a)
[1] 6
> a[which.max(a)]
[1] 8
> which(a==2)
[1] 1 4 9
> which(a>5)
[1] 7 11 13
> a[which(a>5)]
[1] 6 8 7
rev函数,sort函数:
> a<-1:20
> rev(a)
[1] 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
> sort(a)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> sort(rev(a))
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> rev(sort(a))
[1] 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
> a<-c(1:12)
> matrix(a,nrow=3,ncol=4)
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> matrix(a,nrow=4,ncol=3)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> matrix(a,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<-c(1:12)
> matrix(a,nrow=3,ncol=4)
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> matrix(a,nrow=4,ncol=3)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> matrix(a,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
> t(a)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1 2 3 4 5 6 7 8 9 10 11 12
> a
[1] 1 2 3 4 5 6 7 8 9 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
> b=a
> b
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> 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
错误于a %*% b : 非整合参数
> a%*%t(b)
[,1] [,2] [,3]
[1,] 166 188 210
[2,] 188 214 240
[3,] 210 240 270
> diag(a)
[1] 1 5 9
> diag(diag(a))
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 5 0
[3,] 0 0 9
> 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,] 0.2444748 -0.06604418 0.7758382 -1.03614099
[2,] 1.2399238 -0.22550548 -0.9208650 1.32678104
[3,] -0.8085401 -1.48942704 -1.3238675 -1.44913698
[4,] -0.2688544 0.78279487 -0.3427804 -0.04961982
> solve(a)##求逆矩阵
[,1] [,2] [,3] [,4]
[1,] 0.87542310 0.6740288 -0.01689147 0.2358922
[2,] 0.25783626 0.0663873 -0.15839453 1.0169742
[3,] 0.01296783 -0.3604023 -0.31519186 -0.7024465
[4,] -0.76529044 -0.1150571 -0.22989766 -0.5351399
> b<-c(1:4)
> solve(a,b)##解线性方程组
[1] 3.116375 3.983324 -4.463198 -3.825657
矩阵的特征值与特征向量:
> 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