Table of Contents

    Array in R Programming Language: Introduction and Basics

    Array in R Programming Language: Introduction and Basics

    Arrays are the R data objects which can store data in more than two dimensions. For example, − If we create an array of dimension (3, 3, 4) then it creates 4 rectangular matrices each with 3 rows and 3 columns. Arrays can store only data type.

    An array is created using the array() function. It takes vectors as input and uses the values in the dim parameter to create an array.

    Example

    The following example creates an array of two 3x3 matrices each with 3 rows and 3 columns.

     

    # Create two vectors of different lengths.
    vector1

    When we execute the above code, it produces the following result −

    , , 1
    
         [,1] [,2] [,3]
    [1,]   50   10   13
    [2,]    9   11   14
    [3,]    3   12   18
    
    , , 2
    
         [,1] [,2] [,3]
    [1,]   50   10   13
    [2,]    9   11   14
    [3,]    3   12   18

    Naming Columns and Rows

    We can give names to the rows, columns, and matrices in the array by using the dimnames parameter.

     

    # Create two vectors of different lengths.
    vector1

    When we execute the above code, it produces the following result −

    , , Matrix1
    
         COL1 COL2 COL3
    ROW1   50   10   13
    ROW2    9   11   14
    ROW3    3   12   18
    
    , , Matrix2
    
         COL1 COL2 COL3
    ROW1   50   10   13
    ROW2    9   11   14
    ROW3    3   12   18

    Accessing Array Elements

     

    # Create two vectors of different lengths.
    vector1

    When we execute the above code, it produces the following result −

    COL1 COL2 COL3 
       3   12   18 
    [1] 13
         COL1 COL2 COL3
    ROW1   50   10   13
    ROW2    9   11   14
    ROW3    3   12   18

    Manipulating Array Elements

    As array is made up matrices in multiple dimensions, the operations on elements of array are carried out by accessing elements of the matrices.

     

    # Create two vectors of different lengths.
    vector1

    When we execute the above code, it produces the following result −

    [,1] [,2] [,3]
    [1,]   10   20   26
    [2,]   18   22   28
    [3,]    6   24   30

    Calculations Across Array Elements

    We can do calculations across the elements in an array using the apply()function.

    Syntax

    apply(x, margin, fun)

    Following is the description of the parameters used −

    • x is an array.

    • margin is the name of the data set used.

    • fun is the function to be applied across the elements of the array.

    Example

    We use the apply() function below to calculate the sum of the elements in the rows of an array across all the matrices.

     

    # Create two vectors of different lengths.
    vector1

    When we execute the above code, it produces the following result −

    , , 1
    
         [,1] [,2] [,3]
    [1,]    5   10   13
    [2,]    9   11   14
    [3,]    3   12   15
    
    , , 2
    
         [,1] [,2] [,3]
    [1,]    5   10   13
    [2,]    9   11   14
    [3,]    3   12   15
    
    [1] 56 68 60