## What are Arrays in R?

Arrays in R are multidimensional (sometimes called n-d) data structures that hold members of a single data type.

From an implementation point of view, an array is a vector that has one or two additional attributes – dim and dimnames. Only dim attribute is mandatory for an array.

Another way of looking at arrays is to percieve them as a collection of matrices. You will find this notion especially useful if you have to analyze arrays on a two-dimensional medium such as screen or paper. In fact, as we shall soon see, R prints out arrays as if they were a set of matrices organized along multiple dimensions.

Arrays in R are a completely different beast from arrays in C,C++, PHP and Java. Arrays in R are used for solving problem of totally different kind than arrays in register based languages.

Multi-dimensional data structures such as arrays can quickly become cumbersome to comprehend, maintain and debug with increase in dimensions. If you find yourself dealing with arrays with more than three dimensions, you possibly need to go back to drawing board and re-think your implementation strategy or design strategy or most likely both.

## Creating Arrays in R

We have already seen that data structures in R typically have two ways of creation –

- Using the constructor or creator function
- Using the as.* transformer function

Arrays are a little bit special. Arrays can be created by setting attribute(s) of a vector or matrix.

Let’s look at the three ways of creating arrays.

### Creator a.k.a. Constructor Function

You can use the array function to create multi-dimensional arrays. There are two mandatory arguments to this function – an R object contain the constituent elements and a vector by the name dim which specifies length of the array along each dimension.

Let’s look at a trivial example. In this example we have encapsulated our members inside a vector and pass a vector of 3 numerics as dim argument. The vector of three arguments is interpreted as array’s dimensions. For purely understanding convenience, you can interpret the first two numerics in the **dim **vector to be length of rows and columns respectively. The third and subsequent dimensions can be regarded as the number of times the matrix like structure needs to be repeated.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
> > A1 <- array(1:24, dim = c(4, 3, 2) ) > A1 , , 1 [,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12 , , 2 [,1] [,2] [,3] [1,] 13 17 21 [2,] 14 18 22 [3,] 15 19 23 [4,] 16 20 24 > |

Notice that R interprets the dim vector (4,3,2) as command to create an array having 4 rows, 3 columns and a repetition of this matrix like structure twice. A fourth dimension such as 5 in (4, 3, 2, 5) will repeat the structure described in the previous sentence five times.

### Creating Arrays in R By ‘Transformer’ Function

You can use **as.array** to transform other data structures into array. The array() constructor function internally calls as.array to make an array out of other R objects.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
> > M <- matrix(1:4, 2, 2) > M [,1] [,2] [1,] 1 3 [2,] 2 4 > A2 <- as.array(M) > A2 [,1] [,2] [1,] 1 3 [2,] 2 4 > |

### Creating Array from Vector by Setting dim Attribute

This is a special case and is not the typical way of creating new data structures in R. This method relies on the fact that if a vector or a matrix has dim attribute, it starts acting like an array.

1 2 3 4 5 6 7 8 9 10 11 12 |
> > D <- seq(1, 24) > dim(D) <- c(2, 3, 4) > typeof(D) [1] "integer" > class(D) [1] "array" > |

The same methodology can be applied on matrices as well –

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
> > M <- matrix(11:14, 2, 2) > M [,1] [,2] [1,] 11 13 [2,] 12 14 > class(M) [1] "matrix" > attributes(M) $dim [1] 2 2 # convert to array of 2x1 matrix repeated twice > dim(M) <- c(2, 1, 2) > attributes(M) $dim [1] 2 1 2 > class(M) # Matrix is now an array [1] "array" > class(M) [1] "array" > |

Remember that if you already have your data in a matrix or a vector, this is the fastest method of creating an array because you are just adjusting attributes and don’t have to move data around.

### Creating Named Arrays

Like other data structures, arrays allow you to provide name for each dimension. You can set name of each dimension by providing the argument dimnames a list containing vectors of names along each dimension –

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
> > class(M) [1] "array" > M , , 1 [,1] [1,] 11 [2,] 12 , , 2 [,1] [1,] 13 [2,] 14 > dimnames(M) = list ( + c("1st Row", "2nd Row"), + c("The Column"), + c("Repeating Dim-1", "Repeating Dim-2") + ) > M , , Repeating Dim-1 The Column 1st Row 11 2nd Row 12 , , Repeating Dim-2 The Column 1st Row 13 2nd Row 14 > |

## Accessing Elements of Arrays

Elements can be accessed using the ‘[‘ operator and specifying comma separated ranges of dimensions.

When you don’t specify range of a particular dimension, all elements of that dimension are selected. For example, saying arrayName( , ,) for a 3-d array selects all the elements.

### Indexing Arrays Using Multiple Values

You can specify the range of a dimension as a scalar numeric or numeric vector or logical vector and for named arrays even character vector.

Consider the example below –

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
> M , , Repeating Dim-1 The Column 1st Row 11 2nd Row 12 , , Repeating Dim-2 The Column 1st Row 13 2nd Row 14 > M[1, ,] Repeating Dim-1 Repeating Dim-2 11 13 > M[1, ,] # Get the first row across all dimensions Repeating Dim-1 Repeating Dim-2 11 13 > M[1, ,2] # Get the first row and last value in last dimension [1] 13 # Get the first row, but this time use integer vector > M[c(1), ,c(1,2)] Repeating Dim-1 Repeating Dim-2 11 13 # Get the first row, but this time use logical vector > M[c(T,F), ,c(T,T)] Repeating Dim-1 Repeating Dim-2 11 13 # Get the first row, but this time use character vector > M[c('1st Row'), ,c('Repeating Dim-1','Repeating Dim-2')] Repeating Dim-1 Repeating Dim-2 11 13 > |

In all of the examples above we provided either a scalar index or a vector of indexes for each dimension of the array.

### Indexing Arrays Using Single Value

Arrays have a peculiar behavior that when you index them with a single scalar or a single vector. R interprets the single value as index of the underlying vector. This is almost never required in practical R programming. Therefore, watch out for the constructs shown below. Nevertheless, you need to be aware that such oddities do exist.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
> > M , , Repeating Dim-1 The Column 1st Row 11 2nd Row 12 , , Repeating Dim-2 The Column 1st Row 13 2nd Row 14 > M[4] [1] 14 > M[c(1,3)] [1] 11 13 > M[c(T, T, F, T)] [1] 11 12 14 > M[5] [1] NA > |

## Manipulating Array Elements

The methods of accessing array elements described in previous section can also be used to change the elements. You are allowed to reassign values to array elements or even parts of an array after accessing them through ‘[‘.

Consider the trivial example below where we manipulate a single element –

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
> > A3 <- array(101:124, dim = c(2,2,2,2)) > A3 , , 1, 1 [,1] [,2] [1,] 101 103 [2,] 102 104 , , 2, 1 [,1] [,2] [1,] 105 107 [2,] 106 108 , , 1, 2 [,1] [,2] [1,] 109 111 [2,] 110 112 , , 2, 2 [,1] [,2] [1,] 113 115 [2,] 114 116 # Now access and reassign an element of A3 > A3[1,2,2,1] [1] 107 # reassign > A3[1,2,2,1] <- 214 # check > A3[1,2,2,1] [1] 214 > A3 , , 1, 1 [,1] [,2] [1,] 101 103 [2,] 102 104 , , 2, 1 [,1] [,2] [1,] 105 214 [2,] 106 108 , , 1, 2 [,1] [,2] [1,] 109 111 [2,] 110 112 , , 2, 2 [,1] [,2] [1,] 113 115 [2,] 114 116 > |

In the above example we manipulated a single value.

You can treat parts of arrays in R as matrices and manipulate those sections as if they were real matrices.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
> # the array before manipulation > A3 , , 1, 1 [,1] [,2] [1,] 101 103 [2,] 102 104 , , 2, 1 [,1] [,2] [1,] 105 214 [2,] 106 108 , , 1, 2 [,1] [,2] [1,] 109 111 [2,] 110 112 , , 2, 2 [,1] [,2] [1,] 113 115 [2,] 114 116 > A3[, , 1,1] [,1] [,2] [1,] 101 103 [2,] 102 104 # take transpose of a section of array > A3[, , 1, 1] <- t(A3[ , , 1, 1]) # array post transpose > A3 , , 1, 1 [,1] [,2] [1,] 101 102 [2,] 103 104 , , 2, 1 [,1] [,2] [1,] 105 214 [2,] 106 108 , , 1, 2 [,1] [,2] [1,] 109 111 [2,] 110 112 , , 2, 2 [,1] [,2] [1,] 113 115 [2,] 114 116 > |

You can also carry out member-wise addition, subtraction, multiplication and division of an array with a scalar –

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
> # multiply an array by 2 # here we are not assigning the new array to # the original array > A3 * 2 , , 1, 1 [,1] [,2] [1,] 202 204 [2,] 206 208 , , 2, 1 [,1] [,2] [1,] 210 428 [2,] 212 216 , , 1, 2 [,1] [,2] [1,] 218 222 [2,] 220 224 , , 2, 2 [,1] [,2] [1,] 226 230 [2,] 228 232 > |

It is also possible to ‘matrix multiply’ conformable arrays –

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
> > A5 <- array(101:104, dim=c(1,1,2,2)) > A5 , , 1, 1 [,1] [1,] 101 , , 2, 1 [,1] [1,] 102 , , 1, 2 [,1] [1,] 103 , , 2, 2 [,1] [1,] 104 > A6 <- array(201:204, dim=c(2,2,1,1)) > A6 , , 1, 1 [,1] [,2] [1,] 201 203 [2,] 202 204 # Now 'matrix multiply' > A5 %*% A6 [,1] [1,] 83030 |

The above example demonstrate that you can view sections of or even entire array as an extended matrix.

## Conclusion

This discussion on arrays concludes our five part discussion of the basic data structures in R.

We will next look at factors in R.

### We are social

Spread the wordFollow CodingRaptor

## Leave a Reply