- ExcelUnplugged
- November 24, 2021
- 0 Comments

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.

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.

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
```

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
```

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 a matrix from an array by selecting one layer of an array.

```
> m = a[,,1]
```

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!

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

- R Language overview, fast and easy beginner tutorial on R
- ggplot2 Basics, fast and easy beginner tutorial on plotting data in R

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

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

R Beginner Tutorial – Matrices

- November 20, 2021

In previous posts, we learned vectors and lists. Vectors are ...

R Beginner Tutorial – Lists

- November 19, 2021

In previous post, we learned vectors are the most basic data ...

R Beginner Tutorial – Vectors

- November 12, 2021

Vectors are the most basic R data structure. We already know ...

R Beginner Tutorial – R Objects

- November 11, 2021

What are different objects in R? R is an object-oriented language ...