Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R Arrays

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:

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

)

print

(

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

)

)

print

(

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

)

)

print

(

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

)

)

print

(

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

)

)

print

(

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

)

)

print

(

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

print

(

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

print

(

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

print

(

result

[

3

,,

2

]

)

Display the element in the first row and third column of the first matrix in the array

print

(

result

[

1

,

3

,

1

]

)

Output the second matrix

print

(

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

print

(

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

print

(

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:

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

)

)

print

(

new.

array

)

Compute the sum of numbers in the first row of all matrices in the array

result

<-

apply

(

new.

array

,

c

(

1

)

,

sum

)

print

(

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

)

print

(

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

)

print

(

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

)

print

(

result

)

The output of the code above is:



     [,1] [,2]

[1,]    3    9

[2,]    4   10