Array Operations in R Programming

Array Operations in detail

Arrays are R data objects that store data in more than two dimensions. Arrays are n-dimensional data structures. For example, if we create an array of dimensions (2, 3, 3), it creates three rectangular matrices, each with two rows and three columns. They are homogeneous data structures.

To create an array in R, use the function array(). The arguments to this function include a set of elements in vectors and a vector containing the dimensions of the array.

Syntax:

Array_NAME <- array(data, dim = (row_Size, column_Size, matrices, dimnames))

where:

  • data – An input vector given to the array.
  • matrices – Consists of multi-dimensional matrices.
  • row_Size – Number of row elements that an array can store.
  • column_Size – Number of column elements that an array can store.
  • dimnames – Used to change the default names of rows and columns according to user preference.

Example:

# Create the vectors with different lengths
vector1 <- c(5, 7, 9)
vector2 <- c(12, 14, 16, 18, 20, 22)

# Creating an array using these vectors
result <- array(c(vector1, vector2), dim = c(3, 3, 2))
print(result)
Naming Columns and Rows

We can assign names to the rows and columns using dimnames.

Example:

# Creating Vectors
vector1 <- c(2, 4, 6)
vector2 <- c(8, 10, 12, 14, 16, 18)

# Assigning Names to rows and columns
column.names <- c("A", "B", "C")
row.names <- c("X", "Y", "Z")
matrix.names <- c("Table1", "Table2")

# Creating an array with named dimensions
result <- array(c(vector1, vector2), dim = c(3, 3, 2),
                dimnames = list(row.names, column.names, matrix.names))
print(result)

Output:

, , 1

     [,1] [,2] [,3]
[1,]    5   12   18
[2,]    7   14   20
[3,]    9   16   22

, , 2

     [,1] [,2] [,3]
[1,]    5   12   18
[2,]    7   14   20
[3,]    9   16   22
Manipulating Array Elements

An array consists of multiple dimensions, and operations can be performed by accessing elements.

Example:

# Creating vectors
vector1 <- c(3, 6, 9)
vector2 <- c(2, 4, 6, 8, 10, 12)
array1 <- array(c(vector1, vector2), dim = c(3, 3, 2))

# Creating another array
vector3 <- c(1, 3, 5)
vector4 <- c(7, 9, 11, 13, 15, 17)
array2 <- array(c(vector3, vector4), dim = c(3, 3, 2))

# Extracting matrices and adding them
matrix1 <- array1[,,2]
matrix2 <- array2[,,2]
result <- matrix1 + matrix2
print(result)

Output:

[,1] [,2] [,3]
[1,]    9   13   17
[2,]   11   15   19
[3,]   13   19   23
Accessing Array Elements in R

Using index positions in a matrix, any element can be accessed easily. Additionally, elements in an array can be modified using index positions.

Syntax:

Array_Name[row_position, Column_Position, Matrix_Level]

Example:

# Creating Vectors
vector1 <- c(5, 8, 2)
vector2 <- c(14, 7, 9, 6, 11, 3)

# Defining names
column.names <- c("Col1", "Col2", "Col3")
row.names <- c("Row1", "Row2", "Row3")
matrix.names <- c("Matrix1", "Matrix2")

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

print(result)

# Print second row of first matrix
print(result[2,,1])

Output:

, , Matrix1

     Col1 Col2 Col3
Row1    5   14    6
Row2    8    7   11
Row3    2    9    3

, , Matrix2

     Col1 Col2 Col3
Row1    5   14    6
Row2    8    7   11
Row3    2    9    3

Col1 Col2 Col3
   8    7   11
Performing Calculations Across Array Elements

The apply() function is used for performing calculations on array elements.

Syntax:

apply(x, margin, fun)
  • x – an array.
  • margin – dimension specification (1 for rows, 2 for columns).
  • fun – function to be applied to the elements of the array.

Example:

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

# Creating an array
new_array <- array(c(vector1, vector2), dim = c(3, 3, 2))

print(new_array)

# Calculate sum of columns in matrices
result <- apply(new_array, c(2), sum)

print(result)

Output:

, , 1

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

, , 2

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

[1] 28 24 38

Comments

Leave a Reply

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