R语言学习笔记-矩阵的创建与操作方法 发表于 2020-02-06 | 分类于 编程语言 | | 热度 °C 一、创建矩阵1234567891011121314151617181920# 创建两个数字变量var5=sample(31:60,25,replace=T)var6=sample(1:30,25,replace=T)# 创建两个5行5列的矩阵,创建5行5列的矩阵的原因,是为了方便说明问题;A <- matrix(var5,nrow=5)B <- matrix(var6,nrow=5)> A[,1] [,2] [,3] [,4] [,5][1,] 43 32 54 41 56[2,] 56 45 37 57 38[3,] 50 50 40 32 50[4,] 34 56 49 36 42[5,] 56 41 33 40 46> B[,1] [,2] [,3] [,4] [,5][1,] 18 21 23 1 18[2,] 18 5 23 10 5[3,] 26 6 26 8 26[4,] 7 5 7 16 15[5,] 29 20 1 15 2 二、操作矩阵123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147# 结合矩阵A与B的行,A先B后;C <- rbind(A,B)# 结合矩阵A与B的行,B先A后;D <- rbind(B,A)# 结合矩阵A与B的列,A先B后;E <- cbind(A,B)# 结合矩阵A与B的列,B先A后;F <- cbind(B,A)> C[,1] [,2] [,3] [,4] [,5][1,] 43 32 54 41 56[2,] 56 45 37 57 38[3,] 50 50 40 32 50[4,] 34 56 49 36 42[5,] 56 41 33 40 46[6,] 18 21 23 1 18[7,] 18 5 23 10 5[8,] 26 6 26 8 26[9,] 7 5 7 16 15[10,] 29 20 1 15 2> D[,1] [,2] [,3] [,4] [,5][1,] 18 21 23 1 18[2,] 18 5 23 10 5[3,] 26 6 26 8 26[4,] 7 5 7 16 15[5,] 29 20 1 15 2[6,] 43 32 54 41 56[7,] 56 45 37 57 38[8,] 50 50 40 32 50[9,] 34 56 49 36 42[10,] 56 41 33 40 46> E[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10][1,] 43 32 54 41 56 18 21 23 1 18[2,] 56 45 37 57 38 18 5 23 10 5[3,] 50 50 40 32 50 26 6 26 8 26[4,] 34 56 49 36 42 7 5 7 16 15[5,] 56 41 33 40 46 29 20 1 15 2> F[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10][1,] 18 21 23 1 18 43 32 54 41 56[2,] 18 5 23 10 5 56 45 37 57 38[3,] 26 6 26 8 26 50 50 40 32 50[4,] 7 5 7 16 15 34 56 49 36 42[5,] 29 20 1 15 2 56 41 33 40 46# 选取矩阵A中的单个元素> A[2,3][1] 37# 选取整行;> A[3,][1] 50 50 40 32 50# 选取整列;> A[,5][1] 56 38 50 42 46# 选取多行,注意:生成的新矩阵行号改变;> A[c(3,4),][,1] [,2] [,3] [,4] [,5][1,] 50 50 40 32 50[2,] 34 56 49 36 42# 选取多列,注意:生成的新矩阵列号改变;> A[,c(1,3,5)][,1] [,2] [,3][1,] 43 54 56[2,] 56 37 38[3,] 50 40 50[4,] 34 49 42[5,] 56 33 46# 更改矩阵B的行名与列名;> dimnames(B)=list(c("R1","R2","R3","R4","R5"),c("C1","C2","C3","C4","C5"))> BC1 C2 C3 C4 C5R1 18 21 23 1 18R2 18 5 23 10 5R3 26 6 26 8 26R4 7 5 7 16 15R5 29 20 1 15 2# 对矩阵的行和列进行命名后,可以通过行名与列名操作矩阵;# 选取单个元素;> B["R4","C5"][1] 15# 选取整行;> B["R3",]C1 C2 C3 C4 C5 26 6 26 8 26# 选取整列;> B[,"C2"]R1 R2 R3 R4 R5 21 5 6 5 20# 选取多行;> B[c("R1","R2","R4"),]C1 C2 C3 C4 C5R1 18 21 23 1 18R2 18 5 23 10 5R4 7 5 7 16 15# 选取多列;> B[,c("C3","C5")]C3 C5R1 23 18R2 23 5R3 26 26R4 7 15R5 1 2# 对矩阵C进行行列转置(transpose);> G <- t(C)> G[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10][1,] 43 56 50 34 56 18 18 26 7 29[2,] 32 45 50 56 41 21 5 6 5 20[3,] 54 37 40 49 33 23 23 26 7 1[4,] 41 57 32 36 40 1 10 8 16 15[5,] 56 38 50 42 46 18 5 26 15 2# 解构矩阵(Deconstruction);>H <- c(D)> H[1] 18 18 26 7 29 43 56 50 34 56 21 5 6 5 20 32 45 50 56 41 23 23 26 7 1[26] 54 37 40 49 33 1 10 8 16 15 41 57 32 36 40 18 5 26 15 2 56 38 50 42 46# 解构之后就不是矩阵了;> class(D)[1] "matrix"> class(H)[1] "integer"# 转换矩阵E为数据框;I <- as.data.frame(E)> I <- as.data.frame(E)> IV1 V2 V3 V4 V5 V6 V7 V8 V9 V101 43 32 54 41 56 18 21 23 1 182 56 45 37 57 38 18 5 23 10 53 50 50 40 32 50 26 6 26 8 264 34 56 49 36 42 7 5 7 16 155 56 41 33 40 46 29 20 1 15 2# 先看看E的数据类型;> class(E)[1] "matrix"> mode(E)[1] "numeric"> typeof(E)[1] "integer"# 再看一下I,转变成数据框;> class(I)[1] "data.frame"> mode(I)[1] "list"> typeof(I)[1] "list" 本文作者:括囊无誉 本文链接: R/R语言学习笔记-矩阵的创建与操作方法/ 版权声明: 本博客所有文章均为原创作品,转载请注明出处! ------ 本文结束 ------ 坚持原创文章分享,您的支持将鼓励我继续创作! Donate WeChat Pay