R编程中的数组操作
数组是存储二维以上数据的 R 数据对象。数组是 n 维数据结构。例如,如果我们创建一个维度为 (2, 3, 3) 的数组,那么它会创建 3 个矩形矩阵,每个矩阵有 2 行和 3 列。它们是同构数据结构。
现在,让我们看看如何在 R 中创建数组。要在 R 中创建数组,您需要使用名为 array() 的函数。这个 array() 的参数是向量中的元素集,你必须传递一个包含数组维度的向量。
Array_NAME <- array(data, dim = (row_Size, column_Size, matrices, dimnames)
在哪里,
data – An input vector given to the array.
matrices – Consists of multi-dimensional matrices.
row_Size – Number of row elements that an array can store.
column_Size – Number of column elements that an array can store.
dimnames – Used to change the default names of rows and columns according to the user’s preference.
例子:
# Create the vectors with different length
vector1 <- c(1, 2, 3)
vector2 <- c(10, 15, 3, 11, 16, 12)
# taking this vector as input
result <- array(c(vector1, vector2), dim = c(3, 3, 2))
print(result)
输出:
, , 1
[,1] [,2] [,3]
[1,] 1 10 11
[2,] 2 15 16
[3,] 3 3 12
, , 2
[,1] [,2] [,3]
[1,] 1 10 11
[2,] 2 15 16
[3,] 3 3 12
数组操作
命名列和行
我们可以使用 dimnames 为行和列命名。
例子:
# Creating Vectors
vector1 <- c(1, 2, 3)
vector2 <- c(10, 15, 3, 11, 16, 12)
# Giving Names to rows and columns
column.names <- c("COLUMN1", "COLUMN2", "COLUMN3")
row.names <- c("ROW1", "ROW2", "ROW3")
matrix.names <- c("Matrix.NO1", "Matrix.NO2")
# taking this vector as input
result <- array(c(vector1, vector2), dim = c(3, 3, 2),
dimnames = list(row.names, column.names, matrix.names))
print(result)
输出:
, , Matrix.NO1
COLUMN1 COLUMN2 COLUMN3
ROW1 1 10 11
ROW2 2 15 16
ROW3 3 3 12
, , Matrix.NO2
COLUMN1 COLUMN2 COLUMN3
ROW1 1 10 11
ROW2 2 15 16
ROW3 3 3 12
操作数组元素
数组由多个维度组成,操作是通过访问元素来执行的。
例子:
# creating two vectors of different length
# and taking vector as input
vector1 <- c(1, 2, 3)
vector2 <- c(4, 6, 8, 0, 2, 4)
array1 <- array(c(vector1, vector2), dim = c(3, 3, 2))
# creating other array
vector3 <- c(3, 2, 1)
vector4 <- c(2, 4, 6, 8, 3, 5)
array2 <- array(c(vector3, vector4), dim = c(3, 3, 2))
# create matrices and add them
matrix1 <- array1[,,2]
matrix2 <- array2[,,2]
result <- matrix1 + matrix2
print(result)
输出:
[,1] [,2] [,3]
[1,] 4 6 8
[2,] 4 10 5
[3,] 4 14 9
访问数组元素
使用矩阵中的索引位置可以轻松访问任何元素。此外,我们可以使用索引位置更改/更改数组中的元素。
句法:
Array_Name[row_position, Column_Position, Matrix_Level]
例子:
# Creating Vectors
vector1 <- c(1, 2, 3)
vector2 <- c(10, 15, 3, 11, 16, 12)
column.names <- c("COLUMN1", "COLUMN2", "COLUMN3")
row.names <- c("ROW1", "ROW2", "ROW3")
matrix.names <- c("Matrix.NO1", "Matrix.NO2")
# taking vector as input
result <- array(c(vector1, vector2), dim = c(3, 3, 2),
dimnames = list(row.names, column.names, matrix.names))
print(result)
# print third row of second matrix
print(result[3,,2])
输出:
, , Matrix.NO1
COLUMN1 COLUMN2 COLUMN3
ROW1 1 10 11
ROW2 2 15 16
ROW3 3 3 12
, , Matrix.NO2
COLUMN1 COLUMN2 COLUMN3
ROW1 1 10 11
ROW2 2 15 16
ROW3 3 3 12
COLUMN1 COLUMN2 COLUMN3
3 3 12
跨数组元素的计算
apply()函数用于跨数组元素的计算。
句法:
apply(x, margin, fun)
在哪里,
x——一个数组。
边距——使用的数据集的名称。
fun – 应用于数组元素的函数。
例子:
# create two vectors and take them as input in array
vector1 <- c(3, 2, 1)
vector2 <- c(2, 4, 6, 8, 0, 1)
new.array <- array(c(vector1, vector2), dim = c(3, 3, 2))
print(new.array)
# using apply and calculate the sum of rows in matrices
result <- apply(new.array, c(1), sum)
print(result)
输出:
, , 1
[,1] [,2] [,3]
[1,] 3 2 8
[2,] 2 4 0
[3,] 1 6 1
, , 2
[,1] [,2] [,3]
[1,] 3 2 8
[2,] 2 4 0
[3,] 1 6 1
[1] 26 12 16