Data Structures in R Programming : Matrices, Arrays

 


R scripts - 1. Data Structures –

 

a. Matrices

Matrices are the R objects in which the elements are arranged in a two-dimensional rectangular layout. They contain elements of the same atomic types. Though we can create a matrix containing only characters or only logical values, they are not of much use. We use matrices containing numeric elements to be used in mathematical calculations.

 

A Matrix is created using the matrix() function.

 

Syntax

The basic syntax for creating a matrix in R is –

matrix(data, nrow, ncol, byrow, dimnames)

 

Following is the description of the parameters used −

 

       data is the input vector which becomes the data elements of the matrix.

       nrow is the number of rows to be created.

       ncol is the number of columns to be created.

       byrow is a logical clue. If TRUE then the input vector elements are arranged by row.            dimname is the names assigned to the rows and columns.

 

Example –

Create a matrix taking a vector of numbers as input.

# Elements are arranged sequentially by row.

M        <- matrix(c(3:14), nrow = 4, byrow = TRUE) print(M)

 

# Elements are arranged sequentially by column.

N        <- matrix(c(3:14), nrow = 4, byrow = FALSE) print(N)

 

# Define the column and row names.

rownames = c("row1", "row2", "row3", "row4") colnames = c("col1", "col2", "col3")

 

P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames)) print(P)

 

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

     [,1] [,2] [,3]

[1,]    3    4    5

[2,]    6    7    8

[3,]    9   10   11

[4,]   12   13   14

     [,1] [,2] [,3]

[1,]    3    7   11

[2,]    4    8   12

[3,]    5    9   13 [4,]    6   10   14      col1 col2 col3 row1    3    4    5 row2    6    7    8 row3    9   10   11 row4   12   13   14

 

 

Accessing Elements of a Matrix –

Elements of a matrix can be accessed by using the column and row index of the element. We consider the matrix P above to find the specific elements below.

 

# Define the column and row names.

rownames = c("row1", "row2", "row3", "row4")

colnames = c("col1", "col2", "col3")

 

# Create the matrix.

P <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(rownames, colnames))

 

# Access the element at 3rd column and 1st row. print(P[1,3])

 

# Access the element at 2nd column and 4th row. print(P[4,2])

 

# Access only the  2nd row. print(P[2,])

 

# Access only the 3rd column. print(P[,3])

 

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

[1] 5 [1] 13 col1 col2 col3     6    7    8  row1 row2 row3 row4 

   5    8   11   14

 

 

Matrix Computations –

Various mathematical operations are performed on the matrices using the R operators. The result of the operation is also a matrix.

 

The dimensions (number of rows and columns) should be same for the matrices involved in the operation.

 

Matrix Addition & Subtraction – # Create two 2x3 matrices.

matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2) print(matrix1)

 

matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2) print(matrix2)

 

# Add the matrices.

result <- matrix1 + matrix2 cat("Result of addition","\n") print(result)

 

# Subtract the matrices

result <- matrix1 - matrix2 cat("Result of subtraction","\n") print(result)

 

Matrix Multiplication & Division –


Create two 2x3 matrices.

matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2) print(matrix1)

 

matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2) print(matrix2)

 

# Multiply the matrices. result <- matrix1 * matrix2 cat("Result of multiplication","\n")

print(result)

 

# Divide the matrices

result <- matrix1 / matrix2 cat("Result of division","\n") print(result) 

b. Arrays –

Arrays are the R data objects which can store data in more than two dimensions. For example − If we create an array of dimension (2, 3, 4) then it creates 4 rectangular matrices each with 2 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.

 

An array of two 3x3 matrices each with 3 rows and 3 columns.

# Create two vectors of different lengths.

vector1 <- c(5,9,3)

vector2 <- c(10,11,12,13,14,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,]    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

 

 

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(5,9,3) vector2 <- c(10,11,12,13,14,15) 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    5   10   13

ROW2    9   11   14

ROW3    3   12   15

 

, , Matrix2

 

     COL1 COL2 COL3

ROW1    5   10   13

ROW2    9   11   14

ROW3    3   12   15

 

Accessing Array Elements –

# Create two vectors of different lengths.

vector1 <- c(5,9,3) vector2 <- c(10,11,12,13,14,15) 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 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 

   3   12   15 

[1] 13

     COL1 COL2 COL3

ROW1    5   10   13

ROW2    9   11   14

ROW3    3   12   15

 

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 <- c(5,9,3)

vector2 <- c(10,11,12,13,14,15)

 

# 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(9,1,0) vector4 <- c(6,0,11,3,14,1,2,6,9) 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,]   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 <- c(5,9,3)

vector2 <- c(10,11,12,13,14,15)

 

# 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,]    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

 

Post a Comment

Previous Post Next Post