How to check for the dimension of an array in R
Overview
An array in R is simply a data structure that can hold multi-dimensional data of the same type in a single variable. The array object can hold data of two or more dimensions.
Obtaining the dimension of an array
To obtain the dimension of an array in R, we use the dim() function.
Syntax
dim(array)
Parameter value
The dim() function takes a single parameter value that represents the array object.
Return value
The dim() function returns the dimension of the array object.
Example
# creating an array variablemyarray <- c(1:10)# creating a multidimensional arraymultiarray <- array(myarray, dim = c(3,2))# using the dim() function to obtain the dimension of the arraydim(multiarray)
Explanation
- Line 2: We create an array variable
myarraywith its elements ranging from 1 to 10. - Line 5: Using the
array()function, we create a3by2dimensional array. - Line 8: Using the
dim()function, we check for the dimension of the arraymultiarray.
Note: The output of the code above (
3 2) simply means that the array is a 3 by 2 array.