课程表

R语言 基础教程

R语言 图表

R语言 数据接口

R语言 统计示例

工具箱
速查手册

R语言 数组

当前位置:免费教程 » 程序设计 » R语言

数组是可以在两个以上维度中存储数据的R数据对象。 例如 - 如果我们创建一个维度(2,3,4)的数组,则它创建4个矩形矩阵,每个矩阵具有2行和3列。 数组只能存储数据类型。

使用array()函数创建数组。 它使用向量作为输入,并使用dim参数中的值创建数组。

以下示例创建一个由两个3x3矩阵组成的数组,每个矩阵具有3行和3列。

  1. # Create two vectors of different lengths.
  2. vector1 <- c(5,9,3)
  3. vector2 <- c(10,11,12,13,14,15)
  4.  
  5. # Take these vectors as input to the array.
  6. result <- array(c(vector1,vector2),dim = c(3,3,2))
  7. print(result)

当我们执行上面的代码,它产生以下结果 -

  1. , , 1
  2.  
  3. [,1] [,2] [,3]
  4. [1,] 5 10 13
  5. [2,] 9 11 14
  6. [3,] 3 12 15
  7.  
  8. , , 2
  9.  
  10. [,1] [,2] [,3]
  11. [1,] 5 10 13
  12. [2,] 9 11 14
  13. [3,] 3 12 15

命名列和行

我们可以使用dimnames参数给数组中的行,列和矩阵命名。

  1. # Create two vectors of different lengths.
  2. vector1 <- c(5,9,3)
  3. vector2 <- c(10,11,12,13,14,15)
  4. column.names <- c("COL1","COL2","COL3")
  5. row.names <- c("ROW1","ROW2","ROW3")
  6. matrix.names <- c("Matrix1","Matrix2")
  7.  
  8. # Take these vectors as input to the array.
  9. result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,column.names,
  10. matrix.names))
  11. print(result)

当我们执行上面的代码,它产生以下结果 -

  1. , , Matrix1
  2.  
  3. COL1 COL2 COL3
  4. ROW1 5 10 13
  5. ROW2 9 11 14
  6. ROW3 3 12 15
  7.  
  8. , , Matrix2
  9.  
  10. COL1 COL2 COL3
  11. ROW1 5 10 13
  12. ROW2 9 11 14
  13. ROW3 3 12 15

访问数组元素

  1. # Create two vectors of different lengths.
  2. vector1 <- c(5,9,3)
  3. vector2 <- c(10,11,12,13,14,15)
  4. column.names <- c("COL1","COL2","COL3")
  5. row.names <- c("ROW1","ROW2","ROW3")
  6. matrix.names <- c("Matrix1","Matrix2")
  7.  
  8. # Take these vectors as input to the array.
  9. result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,
  10. column.names, matrix.names))
  11.  
  12. # Print the third row of the second matrix of the array.
  13. print(result[3,,2])
  14.  
  15. # Print the element in the 1st row and 3rd column of the 1st matrix.
  16. print(result[1,3,1])
  17.  
  18. # Print the 2nd Matrix.
  19. print(result[,,2])

当我们执行上面的代码,它产生以下结果 -

  1. COL1 COL2 COL3
  2. 3 12 15
  3. [1] 13
  4. COL1 COL2 COL3
  5. ROW1 5 10 13
  6. ROW2 9 11 14
  7. ROW3 3 12 15

操作数组元素

由于数组由多维构成矩阵,所以对数组元素的操作通过访问矩阵的元素来执行。

  1. # Create two vectors of different lengths.
  2. vector1 <- c(5,9,3)
  3. vector2 <- c(10,11,12,13,14,15)
  4.  
  5. # Take these vectors as input to the array.
  6. array1 <- array(c(vector1,vector2),dim = c(3,3,2))
  7.  
  8. # Create two vectors of different lengths.
  9. vector3 <- c(9,1,0)
  10. vector4 <- c(6,0,11,3,14,1,2,6,9)
  11. array2 <- array(c(vector1,vector2),dim = c(3,3,2))
  12.  
  13. # create matrices from these arrays.
  14. matrix1 <- array1[,,2]
  15. matrix2 <- array2[,,2]
  16.  
  17. # Add the matrices.
  18. result <- matrix1+matrix2
  19. print(result)

当我们执行上面的代码,它产生以下结果 -

  1. [,1] [,2] [,3]
  2. [1,] 10 20 26
  3. [2,] 18 22 28
  4. [3,] 6 24 30

跨数组元素的计算

我们可以使用apply()函数在数组中的元素上进行计算。

语法

  1. apply(x, margin, fun)

以下是所使用的参数的说明 -

  • x是一个数组。

  • margin是所使用的数据集的名称。

  • fun是要应用于数组元素的函数。

我们使用下面的apply()函数计算所有矩阵中数组行中元素的总和。

  1. # Create two vectors of different lengths.
  2. vector1 <- c(5,9,3)
  3. vector2 <- c(10,11,12,13,14,15)
  4.  
  5. # Take these vectors as input to the array.
  6. new.array <- array(c(vector1,vector2),dim = c(3,3,2))
  7. print(new.array)
  8.  
  9. # Use apply to calculate the sum of the rows across all the matrices.
  10. result <- apply(new.array, c(1), sum)
  11. print(result)

当我们执行上面的代码,它产生以下结果 -

  1. , , 1
  2.  
  3. [,1] [,2] [,3]
  4. [1,] 5 10 13
  5. [2,] 9 11 14
  6. [3,] 3 12 15
  7.  
  8. , , 2
  9.  
  10. [,1] [,2] [,3]
  11. [1,] 5 10 13
  12. [2,] 9 11 14
  13. [3,] 3 12 15
  14.  
  15. [1] 56 68 60
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号