R Arrays

R Arrays

Unlike matrices, an array can be of any number of dimensions. It means it can store data in more than two dimensions. It also takes vectors as input. To create an array it uses the dim parameter.

Example:

Let’s create an array with two elements with 3X3 matrices each.

Output:

Naming columns and Rows

We can give names to columns, rows, and matrices in the array with the dimnames parameter.

Example:

Output:

Accessing Array Elements

Output:

Manipulating Array Elements

Example:

Output:

Calculations across array elements

To perform calculations across array elements we have to use apply() function.

Syntax:

Here, x is an array

margin is the data set name

fun is the function which you want to apply across the elements of the array

Example:

Let’s see an example to find the sum of the elements in the rows of an array across all the matrices:

Output:

Reference:
https://www.tutorialspoint.com/r/r_arrays.htm
https://www.rdocumentation.org/packages/base/versions/3.5.3/topics/array