R(15), Array

Published: by Creative Commons Licence

Array can only store numeric elements.

We use function array() to create an array.

CREATE

array() functions take vector as inputs and create array using the values in parameter dim.

array(vector, dim)

# Create two vectors of different lengths.
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)

# Take these vectors as input to the array.
result <- array(c(vector1,vector2),dim = c(3,3,2))
print(result)

NAME

dimnames can be used to name the rows, columns, and matrices.

# Create two vectors of different lengths.
vector1 <- c(5,9,3)
vector2 <- c(10,11,12,13,14,15)
column.names <- c("COL1","COL2","COL3")
row.names <- c("ROW1","ROW2","ROW3")
matrix.names <- c("Matrix1","Matrix2")

# Take these vectors as input to the array.
result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,column.names,matrix.names))

VISIT

use the indexes like array visiting.

OPERATION

Operations are carried out by visiting the elements of matrices, because an array is a multi-dimensional matrix.


Apply()

We can use function apply() to carry out the calculation of matrix elements.

apply(x, margin, fun)
  • x: an array
  • margin: the name of dataset
  • fun: the function applied

wanna see examples? click here!