Operations on Arrays in R

Operations on Arrays in R

  • R
  • 3 mins read

Operations on arrays in R programming are used to create, modify, and delete arrays. Array operations are very useful as they allow you to work on array-like data structures. They also have the advantage of being very fast. Here are some of the most commonly used array operations in R programming.

Introduction to Arrays in R

Arrays are multidimensional R objects that contain atoms belonging to the same data type. There are a fixed number of rows and columns in each matrix. The number of rows, columns, and the total number of such matrices are called the dimensions of the matrix. There are a large number of operations that can be performed on arrays in R programming: 

Modification of an item in the array 

The array value can be accessed in R by specifying the dimensions, that is, the row, column, and matrix index, respectively. This value can then be reassigned to a new value by simply using the assignment operator (**** operators in r****). The value is replaced with a new instance.

#creating data 
vec = 1:24
#creating array
#creating 2 matrices with dimensions 4x3
arr = array(vec,
            dim = c(4,3,2))

#printing the array values
print("Original Array")
print(arr)

#accessing the element at specified position 
orignal_val = arr[2,3,1]
#printing the element at specified position 
cat("Original Element value : ", orignal_val)

#reassigning a new value at this index
arr[2,3,1] = 1000 

#printing the array values
print("Modified Array")
print(arr)
modified_val = arr[2,3,1]
cat("Modified Element value : ", modified_val)
Output
[1] "Original Array"
, , 1

     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12

, , 2

     [,1] [,2] [,3]
[1,]   13   17   21
[2,]   14   18   22
[3,]   15   19   23
[4,]   16   20   24
Original Element value :  10
[1] "Modified Array"
, , 1

     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6 1000
[3,]    3    7   11
[4,]    4    8   12

, , 2

     [,1] [,2] [,3]
[1,]   13   17   21
[2,]   14   18   22
[3,]   15   19   23
[4,]   16   20   24
Modified Element value :  1000

Accessing the dimensions of the array

The dim() the method in R can be used to display the dimensions of the two-dimensional matrices and the number of such matrices in R. The following code can be used to gather information :

#creating data 
vec = 1:24
#creating array
#creating 2 matrices with dimensions 4x3
arr = array(vec,
            dim = c(4,3,2))

#printing the dimensions of the array
cat("Dimensions ", dim(arr))
Output
Dimensions  4 3 2

Check for the existence of an element in the array

The element can be checked if it exists in the array or not by using the %in% operator. This operator is used to return a boolean value depending on whether the data element is present in the specified object or not. The syntax to check for element in the array is: 

Syntax

ele %in% obj

Example

#creating data 
vec = 1:24
#creating array
#creating 2 matrices with dimensions 4x3
arr = array(vec,
            dim = c(4,3,2))

#printing the array values
print("Original Array")
print(arr)

#check for presence of value 23
flag1 = 23 %in% arr
cat("23 present : ", flag1)

#check for presence of value 1000
flag2 = 1000 %in% arr
cat("1000 present : ", flag2)
Output
[1] "Original Array"
, , 1

     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12

, , 2

     [,1] [,2] [,3]
[1,]   13   17   21
[2,]   14   18   22
[3,]   15   19   23
[4,]   16   20   24

23 present :  TRUE
1000 present :  FALSE

Applying functions over arrays

Functions can be applied over arrays by using the apply() method, which takes as arguments the function to be applied over the array. The method has the following syntax : 

apply (data , margin , fun )
  • Where, data - The data over which the function is to be applied
  • margin - The dataset to be used
  • fun - the function to be applied

In the following example, the sum is computed row-wise for both matrices, that is, the first value of the output is the summation of the values in row1 of matrix1 as well as matrix2. 

#creating data 
vec = 1:24
#creating array
#creating 2 matrices with dimensions 4x3
arr = array(vec,
            dim = c(4,3,2))

#printing the array values
print("Original Array")
print(arr)

#applying function over array
res <- apply(arr,c(1),sum)
#printing the output of function 
print(res)
Output
[1] "Original Array"
, , 1

     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12

, , 2

     [,1] [,2] [,3]
[1,]   13   17   21
[2,]   14   18   22
[3,]   15   19   23
[4,]   16   20   24

[1] 66 72 78 84