如何在R中创建不同大小的三维数组?
三维数组可以具有不同大小的矩阵,它们不一定是正方形或矩形。同样,数组中的所有元素都是相同的数据类型。要创建不同大小的三维数组,我们需要在数组函数中使用适当数量的行和列。
示例
A1<-array(c(1:4,5:8,9:12),c(2,2,3)) A1
输出结果
, , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 , , 3 [,1] [,2] [1,] 9 11 [2,] 10 12
示例
A2<-array(c(rpois(4,1),rpois(4,5),rpois(4,8)),c(2,2,3)) A2
输出结果
, , 1 [,1] [,2] [1,] 1 2 [2,] 1 0 , , 2 [,1] [,2] [1,] 2 4 [2,] 4 6 , , 3 [,1] [,2] [1,] 8 2 [2,] 5 9
示例
A3<-array(c(rpois(10,1),rpois(10,5),rpois(10,8)),c(2,5,3)) A3
输出结果
, , 1 [,1] [,2] [,3] [,4] [,5] [1,] 1 0 1 1 0 [2,] 2 0 1 1 1 , , 2 [,1] [,2] [,3] [,4] [,5] [1,] 7 4 8 5 4 [2,] 4 4 6 4 6 , , 3 [,1] [,2] [,3] [,4] [,5] [1,] 5 4 11 9 7 [2,] 3 9 10 11 4
示例
A4<-array(c(rnorm(25,1,0.25),rnorm(25,5,3.2),rnorm(25,125,8)),c(5,5,3)) A4
输出结果
, , 1 [,1] [,2] [,3] [,4] [,5] [1,] 0.9965280 1.2496879 1.0314247 0.708599 0.8077254 [2,] 0.7922626 1.0025704 0.7009782 1.142506 0.7445184 [3,] 1.0053649 0.5531278 1.4328092 1.385334 1.0892912 [4,] 0.9793857 1.3234864 0.5553419 1.215801 1.1023305 [5,] 1.3995579 1.0262346 1.2721514 1.394952 0.6184446 , , 2 [,1] [,2] [,3] [,4] [,5] [1,] 6.174299 6.038592 12.470821 1.722607 1.069005 [2,] 3.789770 8.430932 8.747260 7.552801 3.886604 [3,] 1.730295 2.493142 3.465323 3.507519 3.834119 [4,] 6.075908 7.348407 4.118381 6.081857 4.167369 [5,] 4.130724 6.829041 7.469443 3.409544 5.808045 , , 3 [,1] [,2] [,3] [,4] [,5] [1,] 110.6113 119.8429 121.6303 122.9954 140.5715 [2,] 125.5317 122.1797 135.2451 119.5165 122.9406 [3,] 124.0559 119.0249 135.1922 114.0559 112.1363 [4,] 118.1446 119.0530 134.6688 118.8334 124.4825 [5,] 120.2941 109.3362 119.8095 129.1414 127.6529
示例
A5<-array(c(rexp(25,1.25),rexp(25,3.2),rexp(25,1.37)),c(5,5,3)) A5
输出结果
, , 1 [,1] [,2] [,3] [,4] [,5] [1,] 1.3964752 0.14365375 0.1244120 0.006844883 0.1649243 [2,] 0.4146687 0.74798876 1.3172895 0.273072680 1.9675031 [3,] 0.5640029 0.05317204 0.1365023 1.139974748 0.4691554 [4,] 3.4377135 0.40737835 0.1296337 3.827344957 0.3062992 [5,] 0.5489296 0.68912311 0.9960337 1.891826684 0.6505368 , , 2 [,1] [,2] [,3] [,4] [,5] [1,] 0.461391444 0.14704248 1.7219179 0.26808968 0.01277777 [2,] 0.002633461 0.02843441 0.5924807 0.59324483 1.34762650 [3,] 0.296192070 0.38350989 0.3453407 0.14514950 0.21145723 [4,] 0.137173681 0.34683067 0.9046680 0.05949873 0.42690397 [5,] 0.171683422 0.12761626 0.3183557 0.01596812 0.04222600 , , 3 [,1] [,2] [,3] [,4] [,5] [1,] 0.7641526 0.78719016 0.96282362 0.02212902 0.3261842 [2,] 1.1584192 1.20147453 0.03766346 0.30549609 1.1452613 [3,] 0.5849248 1.97291039 0.52678525 0.78435948 0.0107882 [4,] 2.1916248 0.09176414 1.40056593 1.11841176 0.7624021 [5,] 0.1565796 1.35400081 0.26728881 0.36045571 0.3076494