# R数组用法详解

## R数组语法

R数组具有以下语法：

``array_name <- array(data, dim= (row_size, column_size, matrices, dim_names))``

row_size

column_size

dim_names

## 如何创建？

1. 第一步, 我们将创建两个不同长度的向量。
2. 一旦创建了向量, 我们就将这些向量作为数组的输入。

``````#Creating two vectors of different lengths
vec1 <-c(1, 3, 5)
vec2 <-c(10, 11, 12, 13, 14, 15)

#Taking these vectors as input to the array
res <- array(c(vec1, vec2), dim=c(3, 3, 2))
print(res)``````

``````, , 1
[, 1] [, 2] [, 3]
[1, ]    1   10   13
[2, ]    3   11   14
[3, ]    5   12   15

, , 2
[, 1] [, 2] [, 3]
[1, ]    1   10   13
[2, ]    3   11   14
[3, ]    5   12   15``````

## 命名行和列

``````#Creating two vectors of different lengths
vec1 <-c(1, 3, 5)
vec2 <-c(10, 11, 12, 13, 14, 15)

#Initializing names for rows, columns and matrices
col_names <- c("Col1", "Col2", "Col3")
row_names <- c("Row1", "Row2", "Row3")
matrix_names <- c("Matrix1", "Matrix2")

#Taking the vectors as input to the array
res <- array(c(vec1, vec2), dim=c(3, 3, 2), dimnames=list(row_names, col_names, matrix_names))
print(res)``````

``````, , Matrix1

Col1 Col2 Col3
Row1    1   10   13
Row2    3   11   14
Row3    5   12   15

, , Matrix2

Col1 Col2 Col3
Row1    1   10   13
Row2    3   11   14
Row3    5   12   15``````

## 访问数组元素

``````, , Matrix1
Col1 Col2 Col3
Row1    1   10   13
Row2    3   11   14
Row3    5   12   15

, , Matrix2
Col1 Col2 Col3
Row1    1   10   13
Row2    3   11   14
Row3    5   12   15

Col1 Col2 Col3
5   12   15

[1] 13

Col1 Col2 Col3
Row1    1   10   13
Row2    3   11   14
Row3    5   12   15``````

## 元素操纵

``````#Creating two vectors of different lengths
vec1 <-c(1, 3, 5)
vec2 <-c(10, 11, 12, 13, 14, 15)

#Taking the vectors as input to the array1
res1 <- array(c(vec1, vec2), dim=c(3, 3, 2))
print(res1)

#Creating two vectors of different lengths
vec1 <-c(8, 4, 7)
vec2 <-c(16, 73, 48, 46, 36, 73)

#Taking the vectors as input to the array2
res2 <- array(c(vec1, vec2), dim=c(3, 3, 2))
print(res2)

#Creating matrices from these arrays
mat1 <- res1[, , 2]
mat2 <- res2[, , 2]
res3 <- mat1+mat2
print(res3)``````

``````, , 1
[, 1] [, 2] [, 3]
[1, ]    1   10   13
[2, ]    3   11   14
[3, ]    5   12   15

, , 2
[, 1] [, 2] [, 3]
[1, ]    1   10   13
[2, ]    3   11   14
[3, ]    5   12   15

, , 1
[, 1] [, 2] [, 3]
[1, ]    8   16   46
[2, ]    4   73   36
[3, ]    7   48   73

, , 2
[, 1] [, 2] [, 3]
[1, ]    8   16   46
[2, ]    4   73   36
[3, ]    7   48   73

[, 1] [, 2] [, 3]
[1, ]    9   26   59
[2, ]    7   84   50
[3, ]   12   60   88``````

## 跨数组元素的计算

``apply(x, margin, fun)``

``````#Creating two vectors of different lengths
vec1 <-c(1, 3, 5)
vec2 <-c(10, 11, 12, 13, 14, 15)

#Taking the vectors as input to the array1
res1 <- array(c(vec1, vec2), dim=c(3, 3, 2))
print(res1)

#using apply function
result <- apply(res1, c(1), sum)
print(result)``````

``````, , 1
[, 1] [, 2] [, 3]
[1, ]    1   10   13
[2, ]    3   11   14
[3, ]    5   12   15

, , 2
[, 1] [, 2] [, 3]
[1, ]    1   10   13
[2, ]    3   11   14
[3, ]    5   12   15

[1] 48 56 64``````

• 回顶