Arrays are still a vector in R but they have added extra options to them. We can essentially call them “vector structure”. With a vector we have a list of objects in one dimension. With an array we can have any number of dimensions to our data. In the Future 2-dimensional array called a matrix.
We can consider a simple vector to start with
x <- c(1,2,3,4)
This means that x is a vector with 4 elements. This simple vector can be turned into an array by specifying some dimensions on it.
x.array <- array(x, dim=c(2,2)) x.array
## [,1] [,2] ## [1,] 1 3 ## [2,] 2 4
With arrays we have a vector that can then have a vector of dimensional constraints on it.
A regular vector has a single dimension.
A matrix has 2 dimensions
An array can have up to n dimensions.
We can learn about arrays with the following functions:
##  2 2
We can see that our array is a 2×2 matrix.
##  FALSE
##  TRUE
We can also see that R does view these are different objects. There is an array and a vector class.
Properties of Arrays
We can also have R tell us:
Type of elements does our array contain with the typeof() function.
The structure of the array with the str() function.
Other attributes with the attributes() function.
##  "double"
typeof() actually tells you what type of data is stored inside the array.
## num [1:2, 1:2] 1 2 3 4
## $dim ##  2 2
The structure gives a lot of detail about the array and the attributes lets you know that a given attribute is the number of dimensions which is 2×2.
Working with Arrays
As statisticians it is important to know how to work with arrays. Much of our data will be represented by vectors and arrays.
Previously we learned how to extract or remove information from vectors. We can also index arrays but our index takes into account all the dimensions of our array
For example if we wish to take the element out of the first row and first column we can do that by:
##  1
Just like in vectors, we can replace values in an array but using indexing and assigning of values.
x.array[1,1] <- 5 x.array
## [,1] [,2] ## [1,] 5 3 ## [2,] 2 4
Many times we just wish to index a row or a column. An array is of the format:
array[r,c] = 4
##  1 4
Other functions are designed to work with arrays and preserve the structure of it
y.array <- -x.array x.array + y.array
## [,1] [,2] ## [1,] 0 0 ## [2,] 0 0
Many times we wish to have functions act on either just the row or the column and there are many functions built into R for this. For example:
##  8 6
##  7 7