What is the array() function in R?

Arrays hold multi-dimensional data. In R, an array is an object that can hold more than 2-dimensional data.

R- array can only store data if all the data included is of the same type.

Create an array in R

A function named array() creates an array in R. This takes vectors and dim as parameters.

Vectors are the input from the user or programmers. These are the values given to store in an array. At the same time, dim contains the values on which the array is created.

Syntax


array(data = NA, dim = length(data), dimnames = NULL)

Parameters

It takes the following argument values:

  • data: These are vectors used as an input to the array
  • dim: This is the length of the matrix. This involves setting the number of rows, columns, and matrices we want to form.
  • dimnames: This is null by default and gives index numbers as names to the rows, columns, and matrices. We can add the custom names for all of them here.

Return value

This array() function returns an array.

Example

# Creating vectors of different lengths.
Vect1 <- c(9,2,5)
Vect2 <- c(3,6,8,11,16,18)
# Use these vectors as input to the array.
mydata <- array(c(Vect1,Vect2),dim = c(3,3,1))
print(mydata)

Explanation

  • Line 1 and 2: We create the vectors with Vect1 and Vect2 and assign values to them.
  • Line 3: We name the mydata array. After this, we use the values of the vectors as input to the array(). dim=() values tell the array that the resultant matrix should be of three rows, three columns, and one matrix. We can change the values here according to our requirements.
  • Line 4: We display the output with the mydata print. This will show the resulting matrix:

Give custom names to columns and rows

R allows us to give a custom name to the columns and rows of the matrix.

We use the dimnames() function to give the columns, rows, and matrix custom names:

Vect1 <- c(9,2,5)
Vect2 <- c(3,6,8,11,16,18)
# Giving names to columns, rows, and matrix.
col.names <- c("col1","col2","col3")
rows.name <- c("row1","row2","row3")
matrix.name <- c("My Matrix")
# Use these vectors as input to the array.
mydata <- array(c(Vect1,Vect2),dim = c(3,3,1),dimnames = list(rows.name,col.names,matrix.name))
print(mydata)
  • Line 1 and 2: We create vectors named Vect1 and Vect2 and assign values to them.
  • Line 3: We assign column names— col1, col2, col3.
  • Line 4: We assign row names— row1, row2, row3.
  • Line 5: We assign matrix names — My matrix. This is used to give names to the different matrices.
  • Line 6: We give a name to the array (mydata). After that, we use the values of the vectors as input to the array(). dim =() values tell the array that the resultant matrix should be of three rows, three columns, and one matrix. dimnames() arguments tell us to use the values of the rows, columns, and matrix. We can change the values here according to our own requirements.
  • Line 7: We display the output with print(mydata). This will show the resulting matrix:

Access the array elements in R

We want to access the rows, columns, or the whole matrix separately from the combined output. Access the elements of the array using the following statement:


print(array name[row you want to access, column you want to access, matrix you want to access ])

Now, let’s have a look at an example:

Vect1 <- c(9,2,5)
Vect2 <- c(3,6,8,11,16,18)
# Giving names to columns, rows, and matrix.
col.names <- c("col1","col2","col3")
rows.name <- c("row1","row2","row3")
matrix.name <- c("My Matrix")
# Use these vectors as input to the array.
mydata <- array(c(Vect1,Vect2),dim = c(3,3,1),dimnames =list(rows.name,col.names,matrix.name))
print(mydata)
# Print the 2nd column of the first matrix of the array.
print(mydata[,2,1])
# Print the element in the 2nd row and 1st column of the matrix.
print(mydata[2,1,1])
  • Line 8: print(mydata[,2,1] displays the second column of the array’s first matrix.

  • Line 9: print(mydata[2,1,1]) displays the second row and the matrix’s first column. The commas separate the values.

Free Resources