Arrays
In this lesson, we will learn what arrays are in R and how to create them.
Both data structures (vectors and lists) are one dimensional meaning they have only length and no other dimension.
On the plus side, arrays in R language can be n-dimensional. However, like vectors, arrays can store only values having the same data type.
Creating Arrays
In R, arrays can be created using the array()
keyword. Inputs to array()
include concatenated vectors and the dim
argument for creating an array.
Syntax for Creating Arrays
array(<dataVectors>, dim = <vectorOfDimensions>)
Optionally, we can also provide names for each dimension!
Let’s begin with making a simple two-dimensional array with one vector.
Press + to interact
R
myVector <- c (1, 2, 3) # create a vectormyArray <- array (myVector, dim = c(3, 1))print (myArray)
The dimensions of this array are where the number of rows is and the number of columns is ...