### R Fundamentals

##### Arrays

An array in R can have one, two or more dimensions. Array is just a vector stored with additional attributes like dimensions and names for the dimensions, if needed.

All the elements of an array should be of same data type (string, number, character etc).

Also, all the rows of an array should have same length, and rows and columns can have different dimensions.

We can convert a vector into any dimensional array by specifying its dimensions in the array() function. Below, we convert a vector 'x' into an array 'arr' of dimension (4,3), where (4,3) refers to 4 rows and 3 columns:


>  x <- c(10,20,30,40,50,60,70,80,90,100,110,120)
>
>  arr <- array(x, dim=c(4,3))
>
>  arr


[,1] [,2] [,3]
[1,]   10   50   90
[2,]   20   60  100
[3,]   30   70  110
[4,]   40   80  120


Similarly, we can create an array of dimension (2,2,3) as follows:


>  x <- c(10,20,30,40,50,60,70,80,90,100,110,120)
>
>  brr <- array(x, dim=c(2,2,3))
>
>  brr


, , 1

[,1] [,2]
[1,]   10   30
[2,]   20   40

, , 2

[,1] [,2]
[1,]   50   70
[2,]   60   80

, , 3

[,1] [,2]
[1,]   90  110
[2,]  100  120

Note that the array of dimension (2,2,3) is viewed as three arrays, each of dimension (2, 2).

Individual elements of an array can be accessed by giving the name of the array followed by the subscripts in square brackets, separated by commas. For example, in the above arrays, arr[2,1] refers to the element in second row in the first column.

Dropping a subscript of a row or a column will give all elements in the corresponding row or column. Thus, arr[2,] is a vector of elements in the entire second row and arr[,1] is a vector of elements in the entire first column.

In a three dimensional array, brr[1,2,3] refers to the element in the first row, second column of slice 3.