R语言中的数组(Array)本质上是多维数据结构。在数组中,数据以矩阵、行和列的形式存储,我们可以使用矩阵级别、行索引和列索引来访问矩阵元素。
在本文中,我们将介绍如何创建数组、如何访问元素以及如何进行算术运算,并附带示例。
R语言数组语法
数组的语法如下所示。如果您仔细观察下面的数组代码片段,数据是一个向量,并且
- Matrices: 它将决定数组可以接受多少个矩阵。数组始终是多维的,这意味着包含行和列的多个矩阵。
- Row_Size: 请指定可以存储的行元素数量。例如,如果Row_Size = 5,则数组中的每个矩阵将有5行。
- Column_Size: 数组可以存储的列元素数量。例如,Column_Size = 6,则数组中的每个矩阵将有6列。
- dimnames: 用于将默认的行、列和矩阵名称更改为更有意义的名称。
Array_Name <- array(data, dim = (row_Size, column_Size, matrices, dimnames)
在R中创建数组
在此示例中,我们将创建一个数组。以下代码片段将向您展示在R语言中创建数组的最传统方法。
# Create A <- array(1: 24, dim = c(3, 4, 2)) print(A) vect1 <- c(10, 20, 30, 40) vect2 <- c(50, 60, 70, 80, 90, 100) B <- array(c(vect1, vect2), dim = c(3, 3, 2)) print(B)

下面的语句将创建一个包含1到24的元素的数组,这些元素排列在两个矩阵中,每个矩阵包含三行四列。
A <- array(1: 24, dim = c(3, 4, 2))
接下来,我们创建了两个向量。
vect1 <- c(10, 20, 30, 40) vect2 <- c(50, 60, 70, 80, 90, 100)
从上面的截图可以看出,我们使用c函数或连接函数来组合这些向量,以创建包含排列在两个矩阵中的元素的数组。每个矩阵将包含三行三列。
B <- array(c(vect1, vect2), dim = c(3, 3, 2))
为R中的数组定义行名和列名
在本例中,我们将展示如何替换数组中行、列和矩阵的默认名称或为它们定义新名称。我们可以使用dimnames实现相同目的,如下所示:x <- matrix(1:12, dimnames = list(rowNames, columnNames, matrixNames)。
A <- array(1: 24, dim = c(3, 4, 2))
print(A)
# Defining Row names and Column names of Matrix in R
row.names <- c("Row1", "Row2", "Row3")
column.names <-c("Col1", "Col2", "Col3", "Col4")
matrix.names <-c("Matrixl1", "Matrix2")
B <- array(1: 24, dim = c(3, 4, 2), dimnames = list(row.names, column.names, matrix.names))Defining Row names and Column names
print(B)

访问数组元素
在R语言中,我们可以使用索引位置来访问数组元素。通过索引,我们可以访问或修改其中每一个单独的元素。索引值从1开始,到n结束,其中n是矩阵、行或列的大小。
此数组访问背后的语法是
Array_Name[row_position, Column_Position, Matrix_Level].
例如,我们声明了一个包含两个矩阵的数组,每个矩阵的大小为6行*4列。要访问或修改第一个值,请使用Array_name[1, 1, 1];要访问或修改第一个矩阵级别中第二行第三列的值,请使用Array_name[2, 3, 1];要访问第二个矩阵级别中第六行第四列的值,请使用Array_name[6, 4, 2]。让我们通过示例来更好地理解。
# Accessing Elements A <- array(1: 24, dim = c(3, 4, 2)) print(A) # Access the element of 1st row and 2nd column in Matrix 1. print(A[1, 2, 1]) # Access the element of 3rd row and 4th column in in Matrix 2. print(A[3, 4, 2]) # Access only the 3rd row in First Matrix. print(A[3, , 1]) # Access only the 4th column in Second. print(A[, 4, 2]) # Access the Complete First. print(A[ , , 1]) # Access the Complete Second. print(A[ , , 1])

访问数组的所有元素
在此示例中,我们将向您展示访问数组中每个元素的流程。
# Accessing Elements
StudentArray <- array(10: 34, dim = c(3, 4, 2))
print(StudentArray)
print(paste("Element at StudentArray[1, 1, 1] = ", StudentArray[1, 1, 1]))
print(paste("Element at StudentArray[1, 2, 1] = ", StudentArray[1, 2, 1]))
print(paste("Element at StudentArray[1, 3, 1] = ", StudentArray[1, 3, 1]))
print(paste("Element at StudentArray[1, 4, 1] = ", StudentArray[1, 4, 1]))
print(paste("Element at StudentArray[2, 1, 1] = ", StudentArray[2, 1, 1]))
print(paste("Element at StudentArray[2, 2, 1] = ", StudentArray[2, 2, 1]))
print(paste("Element at StudentArray[2, 3, 1] = ", StudentArray[2, 3, 1]))
print(paste("Element at StudentArray[2, 4, 1] = ", StudentArray[2, 4, 1]))
print(paste("Element at StudentArray[1, 1, 2] = ", StudentArray[1, 1, 2]))
print(paste("Element at StudentArray[1, 2, 2] = ", StudentArray[1, 2, 2]))
print(paste("Element at StudentArray[1, 3, 2] = ", StudentArray[1, 3, 2]))
print(paste("Element at StudentArray[1, 4, 2] = ", StudentArray[1, 4, 2]))
print(paste("Element at StudentArray[2, 1, 2] = ", StudentArray[2, 1, 2]))
print(paste("Element at StudentArray[2, 2, 2] = ", StudentArray[2, 2, 2]))
print(paste("Element at StudentArray[2, 3, 2] = ", StudentArray[2, 3, 2]))
print(paste("Element at StudentArray[2, 4, 2] = ", StudentArray[2, 4, 2]))
# To Access Third Row and 1, 2, 3, 4 Columns inside Ist Matrix
print(paste("Element at StudentArray[3, 1, 1] = ", StudentArray[3, 1, 1]))
print(paste("Element at StudentArray[3, 2, 1] = ", StudentArray[3, 2, 1]))
print(paste("Element at StudentArray[3, 3, 1] = ", StudentArray[3, 3, 1]))
print(paste("Element at StudentArray[3, 4, 1] = ", StudentArray[3, 4, 1]))
# To Access Third Row and 1, 2, 3, 4 Columns inside 2nd Matrix
print(paste("Element at StudentArray[3, 1, 1] = ", StudentArray[3, 1, 2]))
print(paste("Element at StudentArray[3, 2, 1] = ", StudentArray[3, 2, 2]))
print(paste("Element at StudentArray[3, 3, 1] = ", StudentArray[3, 3, 2]))
print(paste("Element at StudentArray[3, 4, 1] = ", StudentArray[3, 4, 2]))

访问数组元素的子集
在我们之前的示例中,我们展示了如何从R数组中访问单个元素。在本例中,我们将展示如何从数组中访问多个项目的子集。要实现这一点,我们使用R向量。
提示:负索引位置会从数组中排除这些值。
# Accessing Elements A <- array(1: 24, dim = c(3, 4, 2)) print(A) # Access the elements of 1st, 3rd row and 2nd, 4th column in Matrix 1. print(A[c(1, 2), c(3, 4), 1]) # Access All the element of 2nd and 3rd row in Matrix 2. print(A[c(2, 3), , 2]) # Access All the element of 1st and 4th Column in Matrix 1. print(A[ , c(1, 4), 1]) # Access All the element except 2nd row and 3rd Columm in Matrix 2. print(A[-2, -3, 2])

数组的加法和减法
在此示例中,我们将展示如何使用算术运算符对矩阵执行算术运算。
# Adding and Subtracting Elements vect1 <- c(10, 20, 40 ) vect2 <- c(55, 67, 89, 96, 100) A <- array(c(vect1, vect2), dim = c(3, 4, 2)) print(A) mat.A <- A[, , 1] mat.B <- A[, , 2] print(mat.A + mat.B) print(mat.B - mat.A)
为了执行算术运算,我们将多维矩阵转换为一维矩阵。
mat.A <- A[, , 1] mat.B <- A[, , 2]
