Array in R Programming – Array Function and ways to create array


1. Objective

Through this tutorial, we will be going to learn the concept of array in R. We will also cover example to understand this concept in a better way.Along with this, we will also cover different operations which are performed on rows and columns in an R array.

Array in R programming

2. Introduction to Array in R

We use to call array in R Programming simply called the multi-dimensional Data structure. In this,  data is stored in the form of matrices, row, and as well as in columns. We can use matrix level, row index, and column index to access the matrix elements.

Arrays in R are the data objects which can store data in more than two dimensions. An array is created using the array() function. We can use vectors as input. To create an array we can use this values in the dim parameter.

Knowledge of vector functions is also necessary with the concept of vectors.So, to learn this you can follow the below-mentioned link:

R Vector Functions

For Example:

In this following example, we will create an array in R of two 3×3 matrices each with 3 rows and 3 columns.

# Create two vectors of different lengths.

vector1 <- c(2,9,3)
vector2 <- c(10,16,17,13,11,15)

# Take these vectors as input to the array.

result <- array(c(vector1,vector2),dim = c(3,3,2))
print(result)

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

, , 1


         [,1]   [,2]    [,3]
[1,]      2     10     13
[2,]      9     16     11
[3,]      3     17     15

, , 2

        [,1]   [,2]    [,3]
[1,]       2   10      13
[2,]       9   16      11
[3,]       3   17      15

3. Different Operations on Rows and Columns

3.1 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 <- c(2,9,6)
vector2 <- c(10,15,13,16,11,12)
column.names <- c(“COL1″,”COL2″,”COL3”)
row.names <- c(“ROW1″,”ROW2″,”ROW3”)
matrix.names <- c(“Matrix1″,”Matrix2”)

# Take these vectors as input to the array.

result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,column.names,
  matrix.names))
print(result)

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

, , Matrix1


              COL1   COL2     COL3
ROW1      2          10           16
ROW2      9          15           11
ROW3      6          13           12

, , Matrix2

               COL1   COL2   COL3
ROW1       2         10          16
ROW2       9         15          11
ROW3       6         13          12

3.2 Accessing Array Elements

# We will create two vectors of different lengths.

vector1 <- c(2,9,6)
vector2 <- c(10,15,13,16,11,12)
column.names <- c(“COL1″,”COL2″,”COL3”)
row.names <- c(“ROW1″,”ROW2″,”ROW3”)
matrix.names <- c(“Matrix1″,”Matrix2”)

# Now, we will take these vectors as input to the array.

result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames = list(row.names,column.names,
  matrix.names))
print(result)

# Print the third row of the second matrix of the array.

print(result[3,,2])

# Print the element in the 1st row and 3rd column of the 1st matrix.

print(result[1,3,1])

# Print the 2nd Matrix.

print(result[,,2])

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

COL1 COL2 COL3
  6         13   12
[1] 16
               COL1   COL2   COL3
ROW1       2         10          16
ROW2       9         15          11
ROW3       6         13          12

3.3 Manipulating Array Elements

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

# Create two vectors of different lengths.

vector1 <- c(1,2,3)
vector2 <- c(3,4,5,6,7,8)

# Take these vectors as input to the array.

array1 <- array(c(vector1,vector2),dim = c(3,3,2))

# Create two vectors of different lengths.

vector3 <- c(3,2,1)
vector4 <- c(8,7,6,5,4,3)
array2 <- array(c(vector1,vector2),dim = c(3,3,2))

# create matrices from these arrays.

matrix1 <- array1[,,2]
matrix2 <- array2[,,2]

# Add the matrices.

result <- matrix1+matrix2
print(result)

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

        [,1]       [,2]      [,3]
[1,]      3         11       11
[2,]      4         11       11

[3,]      4         11       11

3.4 Calculations Across R Array Elements

We will be using the apply()function for calculations in an array in R.

Syntax

apply(x, margin, fun)

Following is the description of the parameters used

  • x is an array.
  • a margin is the name of the dataset used.
  • fun is the function to be applied to the elements of the array.

For Example:

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

# We will create two vectors of different lengths.

vector1 <- c(1,2,3)
vector2 <- c(3,4,5,6,7,8)

# Now, we will take these vectors as input to the array.

new.array <- array(c(vector1,vector2),dim = c(3,3,2))
print(new.array)

# Use apply to calculate the sum of the rows across all the matrices.

result <- apply(new.array, c(1), sum)
print(result)

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

, , 1

       [,1]    [,2]    [,3]
[1,]    1        3       6  
[2,]    2        4       7

[3,]    3        5       8

, , 2

       [,1]    [,2]    [,3]
[1,]     1        3       6
[2,]     2        4       7

[3,]     3        5       8

 

[1]  [26] [26] [35]

4. Conclusion

In this respective tutorial, we have studied R array in detail manner with an example for clear understanding of it.We can use an array in R in daily life. Also, we have learned different R array operations which will help you out with the concept of its application.

 

Leave a comment

Your email address will not be published. Required fields are marked *