千家信息网

R语言怎么创建数组

发表于:2025-11-13 作者:千家信息网编辑
千家信息网最后更新 2025年11月13日,本文小编为大家详细介绍"R语言怎么创建数组",内容详细,步骤清晰,细节处理妥当,希望这篇"R语言怎么创建数组"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。数组(array)
千家信息网最后更新 2025年11月13日R语言怎么创建数组

本文小编为大家详细介绍"R语言怎么创建数组",内容详细,步骤清晰,细节处理妥当,希望这篇"R语言怎么创建数组"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

数组(array)是向量和矩阵的推广,是多维(三维或三维以上)数据。与向量和矩阵一样,数组的元素必须也是同一类型的数据。例如 - 如果我们创建一个维度(2,3,4)的数组,则会创建4个矩形矩阵,每个矩阵具有2行和3列。

创建数组

在R中,一般用array()函数来创建数组。array()的原型为:

array(data = NA, dim = length(data), dimnames = NULL)

其中:data给定数组元素,默认情况下是NA;dim用来指定数组的维度,默认情况下是一维数组;dimnames设定各维度的名称,必须是个列表,默认情况下无名称。
例如创建一个由两个3x3矩阵组成的数组,每个矩阵具有3行和3列。

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

运行以上的代码,输出结果如下:

, , 1     [,1] [,2] [,3][1,]    2    7   11[2,]    3    8   11[3,]    5    9   12, , 2     [,1] [,2] [,3][1,]    2    7   11[2,]    3    8   11[3,]    5    9   12

dimnames参数给数组中的行,列和矩阵命名。

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

运行以上的代码,输出结果如下:

, , Matrix1     COL1 COL2 COL3ROW1    2    7   11ROW2    3    8   11ROW3    5    9   12, , Matrix2     COL1 COL2 COL3ROW1    2    7   11ROW2    3    8   11ROW3    5    9   12

访问数组元素

有关如何访问数组元素,请参考以下代码实现 -

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

运行以上的代码,输出结果如下:

COL1 COL2 COL3   5    9   12[1] 11     COL1 COL2 COL3ROW1    2    7   11ROW2    3    8   11ROW3    5    9   12

操纵数组元素

由于数组是由多个维度组成的矩阵,通过访问矩阵的元素来执行数组元素的相关操作。

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

运行以上的代码,输出结果如下:

     [,1] [,2] [,3][1,]    4   14   22[2,]    6   16   22[3,]   10   18   24

跨数组元素计算

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

apply(x, margin, fun)

以下是使用的参数的描述 -

  • x - 是一个数组。

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

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

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

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

运行以上的代码,输出结果如下:

, , 1     [,1] [,2] [,3][1,]    2    7   11[2,]    3    8   11[3,]    5    9   12, , 2     [,1] [,2] [,3][1,]    2    7   11[2,]    3    8   11[3,]    5    9   12[1] 40 44 52

读到这里,这篇"R语言怎么创建数组"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。

0