Home » R Programming » Arrays in R

Arrays in R

In this tutorial, you will learn about what is arrays in R and how to define arrays in R.

Arrays in R

Arrays in R is an atomic (homogeneous) data structure. Matrix is two dimensional object. When there are more than two dimensions, we use array to store such a data. Thus arrays are similar to matrices, but have more than two dimensions.

The general syntax of array() function is

array(data = NA, dim = length(data), dimnames = NULL)
  • data: a vector giving data to fill the array.
  • dim: the dimension attributes for the array to be created.
  • dimnames: dimension names.
arrays in R
arrays in R

Attributes related to array

Some useful functions related to arrays in R are as follows:

Function Description
class(x) Gives the class of object x as array
typeof(x) Gives the type of data stored in x
dim(x) Sets or changes the dimension of x
str(x) Gives the structure of x
nrow(x) Gives the number of rows of a array x
ncol(x) Gives the number of columns of a array x
rownames(x) Gives row names of a x
colnames(x) Gives column names of a x

How to create an array in R?

Creating an empty array

An empty array can be created just by specifying the dimension argument (dim).

To create an empty array of dimension $2\times 3 \times 2$ use array() function with argument dimension dim=c(2,3,2).

Note that in dim=c(2,3,2) the first number indicate the number of rows, second number indicate the number of columns and third number indicate the strata (matrix layer).

EmptyArray <- array(dim=c(2,3,2))

Above code create an empty array of dimension $2\times 3\times 2$. It will create 2 rectangular matrices of 2 rows and 3 columns with elements NA.

EmptyArray
, , 1

     [,1] [,2] [,3]
[1,]   NA   NA   NA
[2,]   NA   NA   NA

, , 2

     [,1] [,2] [,3]
[1,]   NA   NA   NA
[2,]   NA   NA   NA
str(EmptyArray)
 logi [1:2, 1:3, 1:2] NA NA NA NA NA NA ...

Creating an array of zeros using array() function

Many times we need to initialize an array whose all elements are zero. Such array can be created using array() function as follows:

# Create an array 
zeroarray <- array(0, dim = c(2, 3, 2))

The above code create an zeroarray of dimension $2\times 3 \times 2$ with all its elements equals to zero.

# display zeroarray
zeroarray
, , 1

     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0

, , 2

     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
# display class of zeroarray
class(zeroarray)
[1] "array"
# display mode of zeroarray
mode(zeroarray)
[1] "numeric"
# display structure of zeroarray
str(zeroarray)  
 num [1:2, 1:3, 1:2] 0 0 0 0 0 0 0 0 0 0 ...

Creating array using array() function

An array in R can be created using array() function by specifying the data and the dimension.

Let us create an array of dimension $3\times 2\times 2$ from numbers 1 to 12. It will create 2 rectangular matrices of 3 rows and 2 columns.

# Create an array 
myarray_1 <- array(1:12, dim = c(3, 2, 2))
# display myarray_1
myarray_1
, , 1

     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

, , 2

     [,1] [,2]
[1,]    7   10
[2,]    8   11
[3,]    9   12
# display class of myarray_1
class(myarray_1)
[1] "array"
# display mode of myarray_1
mode(myarray_1)
[1] "numeric"
# display dimension names of myarray_1
dimnames(myarray_1)  
NULL

Creating an array with dimension names

Dimension names can also be assigned while creating an array with argument dimnames=list().

# row names
d1 <- c("Row1", "Row2", "Row3")  
# column names
d2 <- c("Col1", "Col2")       
# strata names
d3 <- c("Strata1", "Strata2")       
myarray_1 <- array(1:12, c(3, 2, 2), 
                 dimnames = list(d1, d2, d3))
# display dimension names of myarray_1
dimnames(myarray_1)  
[[1]]
[1] "Row1" "Row2" "Row3"

[[2]]
[1] "Col1" "Col2"

[[3]]
[1] "Strata1" "Strata2"
myarray_1
, , Strata1

     Col1 Col2
Row1    1    4
Row2    2    5
Row3    3    6

, , Strata2

     Col1 Col2
Row1    7   10
Row2    8   11
Row3    9   12

Creating an array from a vector using dim

An array can also be created from a vector by assigning simply the dimension to the given vector.

# create a vector
vec1 <- 1:12
# check whether vec1 is array
is.array(vec1)   
[1] FALSE
# convert vec1 to array of specified dimension
dim(vec1) <- c(3, 2, 2) 
# check whether vec1 is array
is.array(vec1)    
[1] TRUE

Initially vec1 is not an array, but once you change the dimension of vec1 using dim() function, vec1 becomes an array of specified dimension.

# display on the screen
vec1
, , 1

     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

, , 2

     [,1] [,2]
[1,]    7   10
[2,]    8   11
[3,]    9   12

Reshaping existing array

Existing array can be reshaped by assigning a new dimension using dim function.

In earlier example, vec1 is an array of dimension $3\times 2\times 2$. Just by changing the dimension using dim function, an array vec1 can be reshaped to new dimension $2\times 2\times 3$.

# Changing dimension
dim(vec1)<-c(2,2,3)
vec1
, , 1

     [,1] [,2]
[1,]    1    3
[2,]    2    4

, , 2

     [,1] [,2]
[1,]    5    7
[2,]    6    8

, , 3

     [,1] [,2]
[1,]    9   11
[2,]   10   12

Creating array from a vector (Recycling)

Consider a vector data2 of length 10.

data2 <- 1:10
data2
 [1]  1  2  3  4  5  6  7  8  9 10

Suppose we want to create an array of dimension (2,3,2) from vector data2. So total $2 \times 3\times 2 = 12$ elements are required. But the length of data2 is only 10.

The length of vector is shorter than the specified dimension. So the elements of vector are recycled from the beginning again to make it an array of desired dimension.

myarray_2 <- array(data2, dim = c(2, 3, 2))
myarray_2
, , 1

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

, , 2

     [,1] [,2] [,3]
[1,]    7    9    1
[2,]    8   10    2

Another example is about a vector of length larger than the desired dimension of array. If the length of vector is larger than the specified dimension of an array, then additional elements of vector are omitted while creating an array.

data3 <- 1:13
data3
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13
myarray_3 <- array(data3, dim = c(2, 3, 2))
myarray_3
, , 1

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

, , 2

     [,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12

Removing dimension names from an array

Let us recall the array myarray_1 from earlier section.

myarray_1
, , Strata1

     Col1 Col2
Row1    1    4
Row2    2    5
Row3    3    6

, , Strata2

     Col1 Col2
Row1    7   10
Row2    8   11
Row3    9   12

The dimension names can be deleted just by setting the row names (rownames) or column names (colnames) to NULL.

rownames(myarray_1) <- NULL
colnames(myarray_1) <- NULL
myarray_1
, , Strata1

     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

, , Strata2

     [,1] [,2]
[1,]    7   10
[2,]    8   11
[3,]    9   12

All the dimension names can be removed by setting dimension names using dimnames() function to NULL.

dimnames(myarray_1) <- NULL
myarray_1
, , 1

     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

, , 2

     [,1] [,2]
[1,]    7   10
[2,]    8   11
[3,]    9   12

Accessing array elements in R

Accessing array elements using index

Elements of a array can be accessed by specifying row number(s) and/or column number(s) and/or strata number. Like

  • myarray[i, ,] returns $i^{th}$ row,
  • myarray[,j,] returns $j^{th}$ column and
  • myarray[i,j,] returns elements from $i^{th}$ row and $j^{th}$ column and
  • myarray[i,j,k] returns $(i,j,k)^{th}$ element of an array.

That is, if any index position is given as empty, then the full range of that subscript is displayed.

myarray <- array(1:12, dim = c(3, 2, 2),
                 dimnames = list(c("R1","R2","R3"),
                                 c("C1","C2"),
                                 c("S1","S2")))
myarray
, , S1

   C1 C2
R1  1  4
R2  2  5
R3  3  6

, , S2

   C1 C2
R1  7 10
R2  8 11
R3  9 12
# returns 3rd row
myarray[3, ,]  
   S1 S2
C1  3  9
C2  6 12
# OR myarray["R3", , ]
# returns 2nd column
myarray[, 2,] 
   S1 S2
R1  4 10
R2  5 11
R3  6 12
# OR myarray[, "C2",]
# return 1st Strata
myarray[, , 1]
   C1 C2
R1  1  4
R2  2  5
R3  3  6
# OR myarray[, ,"S1"]
# returns 2nd row and 2nd column
myarray[2, 2, ]  
S1 S2 
 5 11 
# returns 2nd column and 2nd strata
myarray[, 2, 2]  
R1 R2 R3 
10 11 12 
# returns 2nd row, 2nd column and 1st strata
myarray[2, 2, 1] 
[1] 5

Accessing adjacent rows or columns or strata

Adjacent rows or columns or strata of an array can be accessed using : operator as follows:

## return 1 to 2 rows and 2nd column
myarray[1:2, 2, ]
   S1 S2
R1  4 10
R2  5 11
## return 2nd row and 1 to 2 column
myarray[2, 1:2, ]
   S1 S2
C1  2  8
C2  5 11

Accessing non-adjacent rows or columns or strata

Non-Adjacent rows or columns or strata of an array can be accessed using c() function as follows:

# return 1st and 3 row and 2 column from the array
myarray[c(1,3), 2, ]
   S1 S2
R1  4 10
R3  6 12

Accessing using negative index

Negative index can be used to remove that row or column or strata and retain the remaining.

# Return except 2nd row but retain 
# 2nd column and all strata
myarray[-2, 2, ]
   S1 S2
R1  4 10
R3  6 12
# Return except 2nd row and 
# 2nd column but all strata
myarray[-2, -2, ]
   S1 S2
R1  1  7
R3  3  9

Endnote

In this tutorial you learned about what is array data structure in R, how to create array and how to access subset of matrix in R with some illustrations.

To learn more about other data structures in R, please refer to the following tutorials:

Hopefully you enjoyed learning this tutorial on array type data structure in R. Hope the content is more than sufficient to understand array in R.

Leave a Comment