R Arrays
Arrays are also objects in the R language; R can create one-dimensional or multidimensional arrays.
An R array is a collection of elements of the same type; the matrix we learned about earlier is actually a two-dimensional array.
The relationship between vectors, matrices, and arrays is shown in the following figure:

R arrays are created using the array() function, which takes a vector as input and can use dim to set the array dimensions.
The syntax of the array() function is as follows:
array(data = NA, dim = length(data), dimnames = NULL)
Parameter Description:
- data - Specify the data source of the array, which can be a vector, matrix, or list.
- dim - Specify the dimensions of the array, which can be an integer vector or a tuple representing dimensions; the default is a one-dimensional array. For example, dim = c(2, 3, 4) creates a 2x3x4 three-dimensional array.
- dimnames Optional parameter used to specify the name of each dimension of the array; it can be a list containing the dimension names.
In R, array indexing starts at 1, which differs from the convention in other programming languages.
In addition, R provides a rich set of functions and operators for working with array data, such as indexing, slicing, reshaping, and aggregation.
In R, you can useMatrixorListto represent multidimensional arrays.
**Matrix:** A matrix is the most commonly used form for representing arrays in R; it is a two-dimensional structure with a fixed number of rows and columns.
You can use the matrix() function to create a matrix, specifying the data elements and dimensions.
Example
Create a 3x3 matrix
my_matrix
<-
matrix
(
c
(
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
)
,
nrow
=
3
,
ncol
=
3
)
(
my_matrix
)
List: A list is a more general form of multidimensional array in R; it can contain elements of different types, and each element can be a matrix, vector, or other data structure.
Example
Create a list containing a matrix and a vector
my_list
<-
list
(
matrix
(
c
(
1
,
2
,
3
,
4
)
,
nrow
=
2
)
,
c
(
5
,
6
,
7
)
)
(
my_list
)
Example
Create a list containing a matrix and a vector
my_list
<-
list
(
matrix
(
c
(
1
,
2
,
3
,
4
)
,
nrow
=
2
)
,
c
(
5
,
6
,
7
)
)
(
my_list
)
Besides matrices and lists, R provides other data structures to represent multidimensional arrays, such as arrays and data frames.
Example
Below are some examples to demonstrate array() the use of the function:
Create a one-dimensional array using a vector:
Example
my_vector
<-
c
(
1
,
2
,
3
,
4
)
my_array
<-
array
(
my_vector,
dim
=
c
(
4
)
)
(
my_array
)
In the following example, we create a two-dimensional array with 3 rows and 3 columns:
Example
Create two vectors of different lengths
vector1
<-
c
(
5
,
9
,
3
)
vector2
<-
c
(
10
,
11
,
12
,
13
,
14
,
15
)
Creating an Array
result
<-
array
(
c
(
vector1,vector2
)
,
dim
=
c
(
3
,
3
,
2
)
)
(
result
)
The output of the code above is:
, , 1
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
, , 2
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
Use the dimnames parameter to set the names of each dimension::
Example
Create two vectors of different lengths
vector1
<-
c
(
5
,
9
,
3
)
vector2
<-
c
(
10
,
11
,
12
,
13
,
14
,
15
)
column.
names
<-
c
(
"COL1"
,
"COL2"
,
"COL3"
)
row.names
<-
c
(
"ROW1"
,
"ROW2"
,
"ROW3"
)
matrix.
names
<-
c
(
"Matrix1"
,
"Matrix2"
)
Create an array and set the names of each dimension
result
<-
array
(
c
(
vector1,vector2
)
,
dim
=
c
(
3
,
3
,
2
)
,
dimnames
=
list
(
row.names
,column.
names
,matrix.
names
)
)
(
result
)
The output of the code above is:
, , Matrix1
COL1 COL2 COL3
ROW1 5 10 13
ROW2 9 11 14
ROW3 3 12 15
, , Matrix2
COL1 COL2 COL3
ROW1 5 10 13
ROW2 9 11 14
ROW3 3 12 15
Accessing Array Elements
In R, you can use the index operator [ ] to access elements of a multidimensional array.
The index operator allows you to retrieve specific elements of an array by their specified index positions.
To get an array element, you can use the element's column index and row index, similar to a coordinate form.
Access a single element:
Example
my_array
<-
array
(
1
:
12
,
dim
=
c
(
2
,
3
,
2
)
)
Create a 3-dimensional array
element
<-
my_array
[
1
,
2
,
1
]
Access the element where the first dimension is 1, the second dimension is 2, and the third dimension is 1
(
element
)
Access multiple elements:
Example
my_array
<-
array
(
1
:
12
,
dim
=
c
(
2
,
3
,
2
)
)
Create a 3-dimensional array
elements
<-
my_array
[
c
(
1
,
2
)
,
c
(
2
,
3
)
,
c
(
1
,
2
)
]
Access multiple elements, where the index of each dimension is 1 and 2 respectively
(
elements
)
Access elements of a two-dimensional array:
Example
Create two vectors of different lengths
vector1
<-
c
(
5
,
9
,
3
)
vector2
<-
c
(
10
,
11
,
12
,
13
,
14
,
15
)
column.
names
<-
c
(
"COL1"
,
"COL2"
,
"COL3"
)
row.names
<-
c
(
"ROW1"
,
"ROW2"
,
"ROW3"
)
matrix.
names
<-
c
(
"Matrix1"
,
"Matrix2"
)
Creating an Array
result
<-
array
(
c
(
vector1,vector2
)
,
dim
=
c
(
3
,
3
,
2
)
,
dimnames
=
list
(
row.names
, column.
names
, matrix.
names
)
)
Display the elements in the third row of the second matrix in the array
(
result
[
3
,,
2
]
)
Display the element in the first row and third column of the first matrix in the array
(
result
[
1
,
3
,
1
]
)
Output the second matrix
(
result
[
,,
2
]
)
The output of the code above is:
COL1 COL2 COL3
3 12 15
[1] 13
COL1 COL2 COL3
ROW1 5 10 13
ROW2 9 11 14
ROW3 3 12 15
Filter using logical conditions:
Example
my_array
<-
array
(
1
:
12
,
dim
=
c
(
2
,
3
,
2
)
)
Create a 3-dimensional array
filtered_elements
<-
my_array
[
my_array
5
]
Select elements greater than 5
(
filtered_elements
)
Output: 6 7 8 9 10 11 12
Manipulating array elements
Since an array is composed of matrices of multiple dimensions, we can access array elements by accessing the elements of the matrices.
Example
Create two vectors of different lengths
vector1
<-
c
(
5
,
9
,
3
)
vector2
<-
c
(
10
,
11
,
12
,
13
,
14
,
15
)
Creating an Array
array1
<-
array
(
c
(
vector1,vector2
)
,
dim
=
c
(
3
,
3
,
2
)
)
Create two vectors of different lengths
vector3
<-
c
(
9
,
1
,
0
)
vector4
<-
c
(
6
,
0
,
11
,
3
,
14
,
1
,
2
,
6
,
9
)
array2
<-
array
(
c
(
vector3,vector4
)
,
dim
=
c
(
3
,
3
,
2
)
)
Create a matrix from an array
matrix1
<-
array1
[
,,
2
]
matrix2
<-
array2
[
,,
2
]
Matrix addition
result
<-
matrix1
matrix2
(
result
)
The output of the code above is:
[,1] [,2] [,3]
[1,] 7 19 19
[2,] 15 12 14
[3,] 12 12 26
In addition, we can use apply() elements to perform cross-dimensional computations on array elements; the syntax is as follows:
apply(X, MARGIN, FUN, ...)
Parameter Description:
X: The array or matrix to which the function is applied.MARGIN: Specifies the dimension to apply the function to; it can be 1 for rows, 2 for columns, or c(1, 2) to apply to both rows and columns.FUN: The function to apply, which can be a built-in function (such asmean、sum, etc.) or a user-defined function....: Optional parameters passed to the function.
Below we use the apply() function to compute the sum of numbers in each row of the two matrices in the array.
Example
Create two vectors of different lengths
vector1
<-
c
(
5
,
9
,
3
)
vector2
<-
c
(
10
,
11
,
12
,
13
,
14
,
15
)
Creating an Array
new.
array
<-
array
(
c
(
vector1,vector2
)
,
dim
=
c
(
3
,
3
,
2
)
)
(
new.
array
)
Compute the sum of numbers in the first row of all matrices in the array
result
<-
apply
(
new.
array
,
c
(
1
)
,
sum
)
(
result
)
The output of the code above is:
, , 1
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
, , 2
[,1] [,2] [,3]
[1,] 5 10 13
[2,] 9 11 14
[3,] 3 12 15
[1] 56 68 60
Apply a built-in function to the rows or columns of a matrix:
Example
Create a 3x3 matrix
my_matrix
<-
matrix
(
1
:
9
,
nrow
=
3
)
Apply the sum function to each column
col_sums
<-
apply
(
my_matrix,
2
,
sum
)
(
col_sums
)
The output of the code above is:
[1] 6 15 24
Apply a user-defined function to the rows or columns of a matrix:
Example
Create a 3x3 matrix
my_matrix
<-
matrix
(
1
:
9
,
nrow
=
3
)
User-defined function: compute the mean of each row
row_mean
<-
function
(
x
)
{
return
(
mean
(
x
)
)
}
Apply the row_mean function to each row
row_means
<-
apply
(
my_matrix,
1
, row_mean
)
(
row_means
)
The output of the code above is:
[1] 4 5 6
Apply a function to multiple dimensions of an array simultaneously:
Example
Create a 3-dimensional array
my_array
<-
array
(
1
:
12
,
dim
=
c
(
2
,
3
,
2
)
)
Apply the mean function to the first and third dimensions simultaneously
result
<-
apply
(
my_array,
c
(
1
,
3
)
,
mean
)
(
result
)
The output of the code above is:
[,1] [,2]
[1,] 3 9
[2,] 4 10