R programming also provides programmers with the facility to store and use memory locations in the form of one or more dimensions. That concept is possible with the use of arrays. In this chapter you will learn about how to use arrays in R program.

## What are Arrays in R programming?

The vector variables that you have implemented at so far on the last tutorial are all single - dimensional / one dimensional objects. This is because they have only length but no other dimensions. Arrays can contain multi dimensional rectangular shaped data storage structure. "Rectangular" in the sense, each row is having the same length and similarly for each column and other dimensions. Matrices are a special type of two - dimensional arrays.

But arrays can store only values having similar kinds of data, i.e. variables / elements having similar data type. An array can be created in R language using the array() function. Arrays take vectors in the form of input and use the values in the dim parameter for creating an array.

Optionally, you can also provide names for each dimension:

```
((thre_d_arr <- array(
1:24,
dim = c (4, 3, 2),
dim_name = list (
c("one", "two", "three", "four"),
c("ray", "karl", "mimo"),
c("steve", "mark")
)
))
```

Another Example:

```
# creating 2 vectors of dissimilar lengths
vec1 <- c (3, 4, 2)
vec2 <- c (11, 12, 13, 14, 15, 16)
# taking these vectors as input for this array
res1 <- array (c (vector1, vector2), dim=c (3,3,2))
print (res1)
```

### Naming Columns and Rows

You can provide names to the rows, columns along with matrices within the array by using the 'dimnames' parameter.

```
# Creating 2 vectors having different lengths.
vec1 <- c (2, 4, 6)
vec2 <- c (11, 12, 13, 14, 15, 16)
column.names <- c ("COLA","COLB","COLC")
row.names <- c ("ROWA","ROWB","ROWC")
matrix.names <- c ("MatA", "MatB")
res1 <- array (c (vector1,vector2), dim=c (3,3,2), dimnames=list (column.names, row.names, matrix.names))
print(res1)
```

The output will be:

MatA ROWA ROWB ROWC COLA 2 11 14 COLB 4 12 15 COLC 6 13 16 MatB ROWA ROWB ROWC COLA 2 11 14 COLB 4 12 15 COLC 6 13 16

## Accessing and Indexing Arrays

Indexing works similarly as it does with vectors, apart from that here you have to state an index for more than 1-dimension. As earlier in lists, you have used square brackets for denoting an index; here you have 4 choices to specify the index (using positive & negative integers, via logical values and using element names).

Here are the lists of examples:

```
res1 <- array (c (vec1,vec2), dim=c (3, 3, 2), dimnames =
list (column.names, row.names, matrix.names))
print (res1 [3,,1])
# this statement prints the 3rd row of the first matrix of the array.
print (result [2,2,1])
# the above statement prints the element in the 2nd row and 2nd column of the 1st matrix.
print (result [,,2])
# the above statement prints the 2nd matrix entirely
```