Excel Unplugged

R Beginner Tutorial – Arrays

In previous posts, we learned vectors, lists and matrices. Let’s see how arrays compare to other data structures. Arrays are level up on vector (1D) and matrix (2D) – they are a 3 dimensional data structure.

R objects and data types: vector, matrix, array, data frame and list
1 Data structures in R. Source: Ceballos and Cardiel 2013.

As seen on the image above, vectors matrices and arrays can contain only one type of elements. Data frames and lists on the other hand, can contain vectors of different types.

Let’s learn how to create and operate with arrays.

Creating arrays

Create an array using array() function.

# Create two vectors.
> v1 = c(1,2,3)
> v2 = c(4, 5, 6, 7, 8, 9)

> # Take these vectors as input to the array.
> a = array(c(v1,v2), dim = c(3,3,2))
> a
, , 1

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

, , 2

     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

> # Show how vector elements get filled to a matrix: we get two different matrices this time!
> v1 = c(1, 2, 3)
> v2 = c(4, 5, 6, 7, 8, 9, 10)

> column_names <- c("col1", "col2", "col3")
> row_names <- c("row1", "row2", "row3")
> matrix_names <- c("m1", "m2")

> a = array(c(v1, v2), dim = c(3, 3, 2), dimnames = list(row_names, column_names, matrix_names))
> a
, , m1

     col1 col2 col3
row1    1    4    7
row2    2    5    8
row3    3    6    9

, , m2

     col1 col2 col3
row1   10    3    6
row2    1    4    7
row3    2    5    8

Naming Columns, Rows and Matrices

Name the column, rows and matrices in the array.

> # Create two vectors of different lengths.
> v1 = c(1,2,3)
> v2 = c(4, 5, 6, 7, 8, 9)
> column_names <- c("col1","col2","col3")
> row_names <- c("row1","row2","row3")
> matrix_names <- c("m1","m2")
> 
> # Take these vectors as input to the array.
> a = array(c(v1,v2),dim = c(3,3,2),dimnames = list(row_names,column_names,
+                                                                   matrix_names))
> a
, , m1

     col1 col2 col3
row1    1    4    7
row2    2    5    8
row3    3    6    9

, , m2

     col1 col2 col3
row1    1    4    7
row2    2    5    8
row3    3    6    9

Accessing Array Elements

Access array elements using element index a[i, j, k].

> # Print the first row of the second matrix of the array.
> a[1,,2]
col1 col2 col3 
  10    3    6 

> # Print the element in the 1st row and 2rd column of the 2nd matrix.
> a[1,2,2]
[1] 3

> # Print the 2nd Matrix.
> a[,,2]
     col1 col2 col3
row1   10    3    6
row2    1    4    7
row3    2    5    8

Create matrix from array

Create a matrix from an array by selecting one layer of an array.

> m = a[,,1]

Sum array rows

Sum the rows across all matrices of an array.

> # Use apply to calculate the sum of the rows across all the matrices.
> sum = apply(a, c(1), sum)
> sum
row1 row2 row3 
  31   27   33 

In conclusion, this is how to operate with arrays. Next up, stay tuned and learn about factors!

In a hurry?

Meanwhile, in a hurry? If you’re already running late, we recommend you head over to our two R crash courses:

For even more amazing tips check out our awesome ExcelOlympics YouTube channel!

References

1 Ceballos, Maite and Nicolás Cardiel. 2013. “Data structure.” First Steps in R. Accessed 2021-10-12.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.