Matrices and Arrays as Vectors with R

Matrices and Arrays as Vectors

In R, matrices and arrays are more complex data structures that can be thought of as multidimensional vectors. Understanding how matrices and arrays operate as vectors is key for advanced data manipulation and analysis.

Matrices

Matrices are two-dimensional structures that store data in rows and columns. They can be conceptualized as vectors in a multidimensional context.

Creating a Matrix

You can create a matrix using the matrix() function.

Create a Matrix

# Create a 2x3 matrix
mat <- matrix(1:6, nrow = 2, ncol = 3)
print(mat)
# Output:
#      [,1] [,2] [,3]
# [1,]    1    3    5
# [2,]    2    4    6

 The matrix() function takes a vector of elements (here 1:6) and organizes them into a matrix with the specified number of rows (nrow) and columns (ncol).

Accessing Matrix Elements

Elements in a matrix can be accessed by row and column indices.

Access a Single Element 

# Access the element in the 1st row and 2nd column
element <- mat[1, 2]
print(element)  # Output: 3

 Access a Whole Row or Column 

# Access the 2nd row
second_row <- mat[2, ]
print(second_row)  # Output: c(2, 4, 6)
# Access the 3rd column
third_col <- mat[, 3]
print(third_col)  # Output: c(5, 6)

 Resizing a Matrix

Matrices can be resized by reorganizing their elements.

Resize a Matrix 

# Create an initial matrix
mat <- matrix(1:6, nrow = 2, ncol = 3)
# Resize to 3x2
new_mat <- matrix(mat, nrow = 3, ncol = 2)
print(new_mat)
# Output:
#      [,1] [,2]
# [1,]    1    4
# [2,]    2    5
# [3,]    3    6

 Arrays

Arrays are multidimensional data structures that can have more than two dimensions.

Creating an Array

You can create an array using the array() function.

Create an Array 

# Create a 3x2x2 array
array_3d <- array(1:12, dim = c(3, 2, 2))
print(array_3d)
# Output:
# , , 1
#
#      [,1] [,2]
# [1,]    1    4
# [2,]    2    5
# [3,]    3    6
#
# , , 2
#
#      [,1] [,2]
# [1,]    7   10
# [2,]    8   11
# [3,]    9   12

 The array() function takes a vector of elements (here 1:12) and arranges them into a multidimensional structure according to the specified dimensions (dim).

Accessing Array Elements

Elements in an array can be accessed by indices for each dimension.

Access a Single Element 

# Access the element at position [2, 1, 2]
element <- array_3d[2, 1, 2]
print(element)  # Output: 8

 Access a Slice of the Array 

# Access the 2nd slice (2nd plane) of the array
slice <- array_3d[, , 2]
print(slice)
# Output:
#      [,1] [,2]
# [1,]    7   10
# [2,]    8   11
# [3,]    9   12

 Resizing an Array

Arrays can also be resized by reorganizing their elements.

Resize an Array 

# Create an initial 3x2x2 array
array_3d <- array(1:12, dim = c(3, 2, 2))
# Resize to 2x3x2
new_array <- array(array_3d, dim = c(2, 3, 2))
print(new_array)
# Output:
# , , 1
#
#      [,1] [,2] [,3]
# [1,]    1    4    7
# [2,]    2    5    8
#
# , , 2
#
#      [,1] [,2] [,3]
# [1,]    3    6    9
# [2,]    4    7   10

Advanced Manipulation

Conversion Between Vectors and Matrices

You can convert between vectors and matrices for various manipulations.

Convert a Vector to a Matrix 

# Create a vector
vec <- 1:12
# Convert to a 3x4 matrix
mat <- matrix(vec, nrow = 3, ncol = 4)
print(mat)
# Output:
#      [,1] [,2] [,3] [,4]
# [1,]    1    4    7   10
# [2,]    2    5    8   11
# [3,]    3    6    9   12

Manipulating Multidimensional Arrays

You can manipulate multidimensional arrays by accessing and modifying specific slices and dimensions.

Manipulate a Slice of an Array 

# Create a 2x3x2 array
array_3d <- array(1:12, dim = c(2, 3, 2))
# Modify elements in the first slice
array_3d[1, , 1] <- c(10, 20, 30)
print(array_3d)
# Output:
# , , 1
#
#      [,1] [,2] [,3]
# [1,]   10   20   30
# [2,]    2    5    8
#
# , , 2
#
#      [,1] [,2] [,3]
# [1,]    7   10   13
# [2,]    8   11   14

In summary, matrices and arrays in R can be thought of as extensions of vectors into higher dimensions. Matrices are two-dimensional structures, while arrays are multidimensional. They use vectors to store their elements and allow for a wide range of manipulations through their multidimensional indexing. Understanding these concepts is crucial for handling complex data structures and performing advanced data analysis in R.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print