1、 数组基本的操作:
1) 、查看数组---直接调用数组名回车;
2) 、按照下标获取数组元素---调用数组名+方括号+元素下标;
3)、查看数组长度(length());
4)、查看数组元素类型(mode(); ps:语言只支持数组元素单一类型,及所有元素要么都是字符,要么都是数值)
5)、对数组的操作还包括多个数组的包括,可以将两个数组组合成一个矩阵(R语言称之为数据框:frame),可以将数组按照行向量组合rbind()和列方向的组合cbind();
> x1=c(1,2,3,4,5)
> x2=c('1','2','3','4','5')
> x1
[1] 1 2 3 4 5
> x1[4]
[1] 4
> x2
[1] "1" "2" "3" "4" "5"
> x2[4]
[1] "4"
> length(x1)
[1] 5
> mode(x1)
[1] "numeric"
> mode(x2)
[1] "character"
> m1<-rbind(x1,x2)
> m1
[,1] [,2] [,3] [,4] [,5]
x1 "1" "2" "3" "4" "5"
x2 "1" "2" "3" "4" "5"
> m2<-cbind(x1,x2)
> m2
x1 x2
[1,] "1" "1"
[2,] "2" "2"
[3,] "3" "3"
[4,] "4" "4"
[5,] "5" "5"</span>
2、有关数组的统计指标处理:
常见的统计指标包括:平均值(mean())、求和(sum())、连乘(prod())、最值(min()、max())、方差(var())、标准差(sd())
首先我们先借助R的快速生成数组函数c(startNum:endNum)生成1到100个数字并存放于数组y内,然后对其进行相应统计指标输出,具体见下图:
> y<-c(1:100)
> mean(y)
[1] 50.5
> sum(y)
[1] 5050
> prod(y)
[1] 9.332622e+157
> max(y)
[1] 100
> min(y)
[1] 1
> var(y)
[1] 841.6667
> sd(y)
[1] 29.01149
3、 关于数据下标的相关处理
R语言提供了实用的下标处理函数,我们可以方便的取出理想数据,例如:
1)、取连续下标的元素;
2)、按照其他数组元素的值取---以其他数组元素的值取出相应本数组对应下标的元素;
3)、按照元素值的大小取,如可以去除数组中大于某个数的所有元素,小于某个数的所有元素等等;
4)、实用的which函数,which在数组下标中代表该数组对象,可以通过which取出数组元素a[which()],相应的也可以取出符合元素的下标which();
5)、自排序函数(sort())和数组反转函数(rev());
<span style="font-weight: normal;"><span style="font-size:14px;">a=c(1,2,3,4,5,8,9,10,15555)
> a[1:5]
[1] 1 2 3 4 5
> a[a<5]
[1] 1 2 3 4
> a[a>4 &a<8]
[1] 5
> a[a[3]]
[1] 3
> which.max(a)
[1] 9
> a[which.max(a)]
[1] 15555
> which(a>1&a<5)
[1] 2 3 4
> a[which(a>1 &a<5)]
[1] 2 3 4
> a[which(a==9)]
[1] 9
> sort(a)
[1] 1 2 3 4 5 8 9 10 15555
> a
[1] 1 2 3 4 5 8 9 10 15555
> rev(a)
[1] 15555 10 9 8 5 4 3 2 1</span></span>
<span style="font-weight: normal;"><span style="font-size:14px;">a=c(1,2,3,4,5,8,9,10,15555)
> a[1:5]
[1] 1 2 3 4 5
> a[a<5]
[1] 1 2 3 4
> a[a>4 &a<8]
[1] 5
> a[a[3]]
[1] 3
> which.max(a)
[1] 9
> a[which.max(a)]
[1] 15555
> which(a>1&a<5)
[1] 2 3 4
> a[which(a>1 &a<5)]
[1] 2 3 4
> a[which(a==9)]
[1] 9
> sort(a)
[1] 1 2 3 4 5 8 9 10 15555
> a
[1] 1 2 3 4 5 8 9 10 15555
> rev(a)
[1] 15555 10 9 8 5 4 3 2 1</span></span>
3、生成矩阵和矩阵操作相关函数:
R提供了数组转矩阵的函数(matrix()),矩阵是后续很多工作的开始:
1)、矩阵进行加减(+-)、相乘(%*%):
Ps:数组生成矩阵时,默认是按照列方向进行,可以加参数byrow=T,使其按行方向生成矩阵
a<-c(1:12)
> mat_a<-matrix(a,nrow=3,ncol=4)
> mat_a
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> mat_aa<-matrix(a,nrow=4,ncol=3)
> mat_aa
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
> mat_b<-matrix(a,nrow=3,ncol=4)
> mat_b
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> mat_a+mat_b
[,1] [,2] [,3] [,4]
[1,] 2 8 14 20
[2,] 4 10 16 22
[3,] 6 12 18 24
> mat_a%*%mat_aa
[,1] [,2] [,3]
[1,] 70 158 246
[2,] 80 184 288
[3,] 90 210 330</span>
2)、取对角线(diag())、转置(t())> mat_a
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> diag(mat_a)
[1] 1 5 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
> t(mat_a)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12
> a=matrix(rnorm(16),4,4)
> a
[,1] [,2] [,3] [,4]
[1,] -1.8060313 -0.1623095 1.6059096 -0.034760390
[2,] -0.5820759 0.5630558 -1.1578085 -0.669633580
[3,] -1.1088896 1.6478175 0.6565885 -0.007604756
[4,] -1.0149620 -0.7733534 2.5489911 1.777084448
> solve(a)
[,1] [,2] [,3] [,4]
[1,] -0.1579445 -0.8872690 0.12951969 -0.3368722
[2,] -0.2696348 -0.1950072 0.61120374 -0.0761404
[3,] 0.4008824 -0.9993255 0.20832490 -0.3678288
[4,] -0.7825607 0.8417821 0.04114366 0.8647852
> b=c(1:4)
> b
[1] 1 2 3 4
> solve(a,b)
[1] -2.8914122 0.8694004 -2.4441089 4.4835754
> 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
R语言的基本数据结构—数据框
数据框也是矩阵形式,但不同于一般的矩阵,数控框中的列可以是不同的数据类型,每一列即为一个属性值,每一行即为条记录,或为一个对象的所有属性的观测值。
> x1=c(1:4)
> x2=c(rnorm(4))
> x3=c('52','72','03','789')
> x4=c(runif(4,min=10000,max=10005))
> y=data.frame(x1,x2,x3,x4)
> y
x1 x2 x3 x4
1 1 -1.1386077 52 10002.51
2 2 1.3678272 72 10004.92
3 3 1.3295648 03 10001.62
4 4 0.3364728 789 10002.41
逻辑循环控制:
R语言支持for循环和while循环,两种循环结构上和通用编程语言类似,但有少数差异。
> a
[1] 1 2 3 4 5 6 7 8 9 10
> for(i in 1:10){a[i]=i*2+5}
> a
[1] 7 9 11 13 15 17 19 21 23 25
> a=c(1:10)
> a
[1] 1 2 3 4 5 6 7 8 9 10
> i=1
> while (a[i]<5){a[i]=a[which.max(a)];i=i+1}
> a
[1] 10 10 10 10 5 6 7 8 9 10