An array in R is a data structure that can store multi-dimensional data of the same type in a single variable. The array object can hold two or more than two-dimensional data.
To understand what a for
loop is in R, click here.
We can loop through the items of an array by using for
loop. Let’s check out the example below:
# creating an array variablemyarray <- c(1:10)# creating a multidimensional arraymultiarray <- array(myarray, dim = c(3, 2))# using a for loopfor(x in multiarray){print(x)}
myarray
with its elements ranging from 1
to 10
.array()
function, we create a 3x2 dimensional array multiarray
.for
loop we make reference to all the elements present in the multiarray
.multiarray
.