创建向量c()
> x=c(1,2,3,4)
> x
[1] 1 2 3 4
查看向量长度length()
> length(x)
[1] 4
数据类型mode()
> mode(x)
[1] "numeric"
均值mean()
求和sun()
最小值min()
最大值max()
求方差var()
连乘prod()
标准差sd()
> 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
> 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]
[1] 13
> a[-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 7 9 11 13
“[]”中如果为向量,则表示下标;如果为逻辑性条件,则输出特定条件元素。
> a[a<20]
[1] 5 7 9 11 13 15 17 19
seq()函数
> seq(5,20)
[1] 5 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
[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
> 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
letters字母向量
which()
> a=rep(2:5,3)
> a
[1] 2 3 4 5 2 3 4 5 2 3 4 5
> which.max(a)
[1] 4
> which.min(a)
[1] 1
> a[which.min(a)]
[1] 2
> which(a==2)
[1] 1 5 9
> which(a>3)
[1] 3 4 7 8 11 12
rev()
> 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
sort()
> a=c(1,8,3,0,4,0,4,8,5,0,7)
> a
[1] 1 8 3 0 4 0 4 8 5 0 7
> sort(a)
[1] 0 0 0 1 3 4 4 5 7 8 8
> rev(a)
[1] 7 0 5 8 4 0 4 0 3 8 1
> rev(sort(a))
[1] 8 8 7 5 4 4 3 1 0 0 0
matrix()
> 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
> matrix(a,nrow=4,ncol=3,byrow=F)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
矩阵运算t(),矩阵加减
> 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
> t(matrix(a,nrow=4,ncol=3,byrow=F))
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
diag()
> 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
>
正态分布随机数rnorm(),逆矩阵solve()
> a=matrix(rnorm(16),4,4)
> a
[,1] [,2] [,3] [,4]
[1,] -1.54259118 1.3176449 0.9563522 -0.5035544
[2,] 0.05807243 1.1320155 0.1725380 -1.0957068
[3,] 0.35827596 -1.0891014 -0.2410851 -0.0136876
[4,] 0.58530585 -0.4879238 -2.4095987 0.5460303
> solve(a)
[,1] [,2] [,3] [,4]
[1,] -0.8933315 0.2854584 -0.6641716 -0.26766499
[2,] -0.2357497 0.1335195 -1.0751868 0.02356783
[3,] -0.2438823 -0.1346265 -0.2108326 -0.50034762
[4,] -0.3293118 -0.7807789 -1.1792158 -0.06862583
>
正态分布函数rnorm( )
泊松分布函数rpois( )
指数分布函数rexp( )
Gamma分布函数rgamma( )
均匀分布函数runif( )
二项分布函数rbinom( )
几何分布函数rgeom( )
solve()也可以解方程
> b=c(1:4)
> solve(a,b)
[1] -3.385590 -3.100000 -3.147023 -5.703020
eigen()特征值与特征向量
> 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.000000e+00 0.0000000 0.8660254
[2,] -0.5 -6.408849e-17 0.8164966 -0.2886751
[3,] -0.5 -7.071068e-01 -0.4082483 -0.2886751
[4,] -0.5 7.071068e-01 -0.4082483 -0.2886751
array数组,矩阵是数组的二维情况
> x=c(1:6)
> is.vector(x)
[1] TRUE
> is.array(x)
[1] FALSE
> dim(x)<-c(2,3)
> x
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> is.array(x)
[1] TRUE
> is.matrix(x)
[1] TRUE
数据框,矩阵形式,但列可以不同数据data.frame()
> x1=c(10,13,45,26,23,12)
> x2=c(12,45,25,64,23,11)
> x=data.frame(x1,x2)
> x
x1 x2
1 10 12
2 13 45
3 45 25
4 26 64
5 23 23
6 12 11
> > x=data.frame('重量'=x1,'运费'=x2)
> x
重量 运费
1 10 12
2 13 45
3 45 25
4 26 64
5 23 23
6 12 11
plot() 绘图函数,后面详细阐述
读取文本数据
- 设置工作目录
- 文件放入工作目录
不在工作目录需用路径 - read.table()
读excle文件 - 文件另存为prn或csv可直接读取 read.prn read.csv
- 安装RODBC——library(RODBC)
循环语句for()
for(i in 1:59)(a[i]=i*2+3)
> 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
>
while()
a=1:120
a[1]=5
> i=1
> while(a[i]<60){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
[19] 41 43 45 47 49 51 53 55 57 59 61 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
[109] 109 110 111 112 113 114 115 116 117 118 119 120
R脚本
source("D:\\h.r")
print()函数
apply()函数
apply(X, MARGIN, FUN, ...)