R语言第二章
2.1 数据类型
数据类型分为:
1.整数型(integer)
x1<-33L;print(class(x1))
#out: [1] "integer"
2.数值型(numeric)
x1<-24;print(class(x1))
#out:[1] "numeric"
3.逻辑型(logical)
x1<-TRUE;class(x1)
#out: [1] "logical"
4.字符型(character)
x1<-'sifsfj';class(x1)
#out:[1] "character"
5.复数型(complex)
x1<-3+2i;class(x1)
#out:[1] "complex"
说明:class()是查看数据类型的函数,以及“;“的作用是如果你想一行写多条命令的话就需要‘‘;”,注释主要由于对代码的说明其符号为 #,增加当前行注释使用Shift+Ctrl+C 以及直接输入#符号
2.2 结构数据类型
结构数据类型:向量,数据框,数组,矩阵,列表等等。
2.2.1矩阵
矩阵是二维结构
1.创建矩阵方法:matrix(data,nrow,ncol=,byrow=T),其中data是矩阵的元素,nrow为行维数,ncol为列的维数。
x1<-matrix(1:40,5,8);x1
#out: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#[1,] 1 6 11 16 21 26 31 36
#[2,] 2 7 12 17 22 27 32 37
#[3,] 3 8 13 18 23 28 33 38
#[4,] 4 9 14 19 24 29 34 39
#[5,] 5 10 15 20 25 30 35 40
#生成5行8列的矩阵,其中1,2,3,4,5....默认以列排的。
x1<-matrix(1:40,5,8,byrow=T);x1
#out:# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
# [1,] 1 2 3 4 5 6 7 8
# [2,] 9 10 11 12 13 14 15 16
# [3,] 17 18 19 20 21 22 23 24
# [4,] 25 26 27 28 29 30 31 32
# [5,] 33 34 35 36 37 38 39 40
#byrow=T,作用就是将矩阵按行排,当T为F时就是第一个的结果,系统默认为F
#当然x1<-matrix(1:40,nrow=5,ncol=8);x1结果与第一个结果相同
另外一种构成矩阵的方法:rbind(c1,c2,…cn),表示按行拼接(row bind),cbind()按列拼接。
rbind(c(1,2,3),c(2,4,6))
#out: [,1] [,2] [,3]
#[1,] 1 2 3
#[2,] 2 4 6
2.矩阵的索引
- 方法一
x<-matrix(1:20,4,5);x
#out: [,1] [,2] [,3] [,4] [,5]
#[1,] 1 5 9 13 17
#[2,] 2 6 10 14 18
#[3,] 3 7 11 15 19
#[4,] 4 8 12 16 20
x[2,] #x[i,] 返回矩阵的第i行
#[1] 2 6 10 14 18
x[,2]#x[,i]返回矩阵的第i列
#[1] 5 6 7 8
x[2,3]#x[i,j]返回矩阵的第i行,第j列
#[1] 10
x[2,-3]##x[i,-j]返回矩阵的第i行,排除第j列
#[1] 2 6 14 18
- 方法二
利用向量来索引
x<-matrix(1:20,4,5);x
#out: [,1] [,2] [,3] [,4] [,5]
#[1,] 1 5 9 13 17
#[2,] 2 6 10 14 18
#[3,] 3 7 11 15 19
#[4,] 4 8 12 16 20
x[c(1,3,4),c(1,3)]#返回1,3,4行,1,3列
# [,1] [,2]
#[1,] 1 9
#[2,] 3 11
#[3,] 4 12
x[c(1,3,4),-c(1,3)]#返回1,3,4行以及排除1,3列
# [,1] [,2] [,3]
#[1,] 5 13 17
#[2,] 7 15 19
#[3,] 8 16 20
3.矩阵运算
as.vector(y):将矩阵转化为向量
t(y):矩阵的转置
nrow(y):返回矩阵行数
ncol(y):返回矩阵列数
colSums(y):对矩阵各列求和
rowSums(y):对矩阵各行求和
colMeans(y):对矩阵各列求平均值
rowMeans(y):对矩阵各行求平均值
diag(y):返回对角元素
y<-matrix(1:16,4,4);y
# [,1] [,2] [,3] [,4]
#[1,] 1 5 9 13
#[2,] 2 6 10 14
#[3,] 3 7 11 15
#[4,] 4 8 12 16
as.vector(y)
#[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
t(y)
# [,1] [,2] [,3] [,4]
#[1,] 1 2 3 4
#[2,] 5 6 7 8
#[3,] 9 10 11 12
#[4,] 13 14 15 16
nrow(y)
#[1] 4
colSums(y)
#[1] 10 26 42 58
rowMeans(y)
#[1] 7 8 9 10
diag(y)
#[1] 1 6 11 16
2.2.2数组
数组的维数可大于2,和矩阵相同每个元素只能拥有一个基本类型数据类型
array(data,dimensions) 其中data给出数组元素,dimensions给出了各个维度下标的最大值。
x<-array(1:24,c(2,3,4));x#创建三维数组
#, , 1
# [,1] [,2] [,3]
#[1,] 1 3 5
#[2,] 2 4 6
#, , 2
# [,1] [,2] [,3]
#[1,] 7 9 11
#[2,] 8 10 12
#, , 3
# [,1] [,2] [,3]
#[1,] 13 15 17
#[2,] 14 16 18
#, , 4
# [,1] [,2] [,3]
#[1,] 19 21 23
#[2,] 20 22 24
###其中c(2,3,4)表示2行,3列,4组
#数组索引与矩阵相同
x[1,2,3]
#out:15
#
> a<-array(rep(1:3,each=9),dim=c(3,3,3));a
#, , 1
# [,1] [,2] [,3]
#[1,] 1 1 1
#[2,] 1 1 1
#[3,] 1 1 1
#, , 2
# [,1] [,2] [,3]
#[1,] 2 2 2
#[2,] 2 2 2
#[3,] 2 2 2
#, , 3
# [,1] [,2] [,3]
#[1,] 3 3 3
#[2,] 3 3 3
#[3,] 3 3 3
rowSums(a)
#[1] 18 18 18
rowSums(a,dims=1)
#[1] 18 18 18
rowSums(a,dims=2)
# [,1] [,2] [,3]
#[1,] 6 6 6
#[2,] 6 6 6
#[3,] 6 6 6
colSums(a)
# [,1] [,2] [,3]
#[1,] 3 6 9
#[2,] 3 6 9
#[3,] 3 6 9
colSums(a,dims=2)
#[1] 9 18 27
数组b的三维图为:
2.2.3数据框
数据框是二维结构,与矩阵不同的是它可允许不同的数据类型放在一个列中
- 创建数据框
x<-data.frame(colname1=col1,colname2=col2,…)
col1,col2,为列向量。
x<-data.frame(wupin=c('apple','banans','pen'),color=c('red','yellow','black') num=c(2,4,5))
x
#out: wupin color num
# 1 apple red 2
# 2 banans yellow 4
# 3 pen black 5
- 数据框索引
和矩阵相同
x[,2]
#[1] "red" "yellow" "black"
x$color
#[1] "red" "yellow" "black"
#x$color 其通过变量引用数据框元素法,
#想当然的理解为:相当于索引变量的[],
- 修改列/行的名字
1.通过colnames(<数据框>)来读取并编辑列名称
2 通过row.names(<数据框>)来读取并编辑行名称
colnames(x)[1]<-'things';x
#out things color num
#1 apple red 2
#2 banans yellow 4
#3 pen black 5
colnames(x)<-c('1','2','3');x
# 1 2 3
#1 apple red 2
#2 banans yellow 4
#3 pen black 5
row.names(x)[1]<-'row1';x
# 1 2 3
#row1 apple red 2
#2 banans yellow 4
#3 pen black 5