 GeeksforGeeks App
Open App Browser
Continue

Arrays are the R data objects which store the data in more than two dimensions. Arrays are n-dimensional data structures. For example, if we create an array of dimensions (2, 3, 3) then it creates 3 rectangular matrices each with 2 rows and 3 columns. They are homogeneous data structures.

Now, let’s see how to create arrays in R. To create an array in R you need to use the function called array(). The arguments to this array() are the set of elements in vectors and you have to pass a vector containing the dimensions of the array.

`Array_NAME <- array(data, dim = (row_Size, column_Size, matrices, dimnames)`

where,

data – An input vector given to the array.
matrices – Consists of multi-dimensional matrices.
row_Size – Number of row elements that an array can store.
column_Size – Number of column elements that an array can store.
dimnames – Used to change the default names of rows and columns according to the user’s preference.

Example:

 `# Create the vectors with different length``vector1 <``-` `c(``1``, ``2``, ``3``)``vector2 <``-` `c(``10``, ``15``, ``3``, ``11``, ``16``, ``12``)`` ` `# taking this vector as input``result <``-` `array(c(vector1, vector2), dim ``=` `c(``3``, ``3``, ``2``))``print``(result)`

Output:

```, , 1

[,1] [,2] [,3]
[1,]    1   10   11
[2,]    2   15   16
[3,]    3    3   12

, , 2

[,1] [,2] [,3]
[1,]    1   10   11
[2,]    2   15   16
[3,]    3    3   12
```

### Operations on Arrays

#### Naming columns and rows

We can give names to the rows and columns using dimnames.
Example:

 `# Creating Vectors``vector1 <``-` `c(``1``, ``2``, ``3``)``vector2 <``-` `c(``10``, ``15``, ``3``, ``11``, ``16``, ``12``)`` ` `# Giving Names to rows and columns``column.names <``-` `c(``"COLUMN1"``, ``"COLUMN2"``, ``"COLUMN3"``)``row.names <``-` `c(``"ROW1"``, ``"ROW2"``, ``"ROW3"``)``matrix.names <``-` `c(``"Matrix.NO1"``, ``"Matrix.NO2"``)`` ` `# taking this vector as input``result <``-` `array(c(vector1, vector2), dim ``=` `c(``3``, ``3``, ``2``), ``                  ``dimnames ``=` `list``(row.names, column.names, matrix.names))``print``(result)`

Output:

```, , Matrix.NO1

COLUMN1 COLUMN2 COLUMN3
ROW1       1      10      11
ROW2       2      15      16
ROW3       3       3      12

, , Matrix.NO2

COLUMN1 COLUMN2 COLUMN3
ROW1       1      10      11
ROW2       2      15      16
ROW3       3       3      12
```

#### Manipulating array elements

An array is made up of multiple dimensions and the operations are carried out by accessing elements.

Example:

 `# creating two vectors of different length``# and taking vector as input``vector1 <``-` `c(``1``, ``2``, ``3``)``vector2 <``-` `c(``4``, ``6``, ``8``, ``0``, ``2``, ``4``)``array1 <``-` `array(c(vector1, vector2), dim ``=` `c(``3``, ``3``, ``2``))`` ` `# creating other array``vector3 <``-` `c(``3``, ``2``, ``1``)``vector4 <``-` `c(``2``, ``4``, ``6``, ``8``, ``3``, ``5``)``array2 <``-` `array(c(vector3, vector4), dim ``=` `c(``3``, ``3``, ``2``))`` ` `# create matrices and add them``matrix1 <``-` `array1[,,``2``]``matrix2 <``-` `array2[,,``2``]``result <``-` `matrix1 ``+` `matrix2``print``(result)`

Output:

```     [,1] [,2] [,3]
[1,]    4    6    8
[2,]    4   10    5
[3,]    4   14    9
```

#### Accessing Array elements

Using index position in matrix any element can be accessed easily. Also, we can alter/change the element in an array using index position.
Syntax:

`Array_Name[row_position, Column_Position, Matrix_Level]`

Example:

 `# Creating Vectors``vector1 <``-` `c(``1``, ``2``, ``3``)``vector2 <``-` `c(``10``, ``15``, ``3``, ``11``, ``16``, ``12``)``column.names <``-` `c(``"COLUMN1"``, ``"COLUMN2"``, ``"COLUMN3"``)``row.names <``-` `c(``"ROW1"``, ``"ROW2"``, ``"ROW3"``)``matrix.names <``-` `c(``"Matrix.NO1"``, ``"Matrix.NO2"``)`` ` `# taking vector as input``result <``-` `array(c(vector1, vector2), dim ``=` `c(``3``, ``3``, ``2``), ``                  ``dimnames ``=` `list``(row.names, column.names, matrix.names))``print``(result)`` ` `# print third row of second matrix``print``(result[``3``,,``2``])`

Output:

```, , Matrix.NO1

COLUMN1 COLUMN2 COLUMN3
ROW1       1      10      11
ROW2       2      15      16
ROW3       3       3      12

, , Matrix.NO2

COLUMN1 COLUMN2 COLUMN3
ROW1       1      10      11
ROW2       2      15      16
ROW3       3       3      12

COLUMN1 COLUMN2 COLUMN3
3       3      12
```

#### Calculation across array element

apply() function is used for calculations across array elements.

Syntax:

`apply(x, margin, fun)`

where,
x – an array.
margin – name of the dataset used.
fun – function to be applied to the elements of the array.

Example:

 `# create two vectors and take them as input in array``vector1 <``-` `c(``3``, ``2``, ``1``)``vector2 <``-` `c(``2``, ``4``, ``6``, ``8``, ``0``, ``1``)``new.array <``-` `array(c(vector1, vector2), dim ``=` `c(``3``, ``3``, ``2``))``print``(new.array)`` ` `# using apply and calculate the sum of rows in matrices``result <``-` `apply``(new.array, c(``1``), ``sum``)``print``(result)`

Output:

```, , 1

[,1] [,2] [,3]
[1,]    3    2    8
[2,]    2    4    0
[3,]    1    6    1

, , 2

[,1] [,2] [,3]
[1,]    3    2    8
[2,]    2    4    0
[3,]    1    6    1

 26 12 16
```

My Personal Notes arrow_drop_up