R Arrays

Introduction

In this chapter, you will learn about arrays in R. Arrays are multi-dimensional data structures that can hold numeric, character, or logical data. Unlike vectors and matrices, arrays can have more than two dimensions. Understanding how to create, manipulate, and perform operations on arrays is essential for handling complex data structures in R.

Creating Arrays

Arrays can be created using the array() function, which arranges data into multiple dimensions.

Example: Creating an Array

Example:

# Creating a 3-dimensional array
arr <- array(1:12, dim = c(3, 2, 2))
print(arr)

Accessing Elements in Arrays

You can access elements in an array using square brackets [] with the indices for each dimension.

Example: Accessing Elements in an Array

Example:

# Accessing elements
print(arr[1, 2, 1])  # Element at first row, second column, first matrix
print(arr[3, , 2])  # All elements of the third row in the second matrix
print(arr[, 1, ])  # All elements of the first column across all matrices

Modifying Elements in Arrays

You can modify elements in an array by specifying the indices and assigning a new value.

Example: Modifying Elements in an Array

Example:

# Modifying elements
arr[1, 2, 1] <- 20
arr[3, , 2] <- c(21, 22)
print(arr)

Array Operations

Arrays support various operations such as addition, subtraction, multiplication, and division. These operations are performed element-wise.

Example: Array Operations

Example:

# Creating arrays for operations
arr1 <- array(1:8, dim = c(2, 2, 2))
arr2 <- array(9:16, dim = c(2, 2, 2))

# Addition
add_result <- arr1 + arr2
print(add_result)

# Subtraction
sub_result <- arr1 - arr2
print(sub_result)

# Element-wise multiplication
mul_result <- arr1 * arr2
print(mul_result)

# Element-wise division
div_result <- arr1 / arr2
print(div_result)

Array Functions

R provides several built-in functions for working with arrays, such as dim(), dimnames(), and aperm().

Example: Using Array Functions

Example:

# Array functions
arr <- array(1:12, dim = c(3, 2, 2))

# Dimensions of the array
print(dim(arr))  # Output: 3 2 2

# Setting dimension names
dimnames(arr) <- list(c("Row1", "Row2", "Row3"), c("Col1", "Col2"), c("Matrix1", "Matrix2"))
print(arr)

# Permuting the dimensions of the array
perm_arr <- aperm(arr, c(2, 1, 3))
print(perm_arr)

Example Program Using Arrays

Here is an example program that demonstrates the use of arrays in R:

# R Program to Demonstrate Arrays

# Creating a 3-dimensional array
arr <- array(1:12, dim = c(3, 2, 2))
print("Array:")
print(arr)

# Accessing elements
print("Element at (1, 2, 1):")
print(arr[1, 2, 1])

print("All elements of the third row in the second matrix:")
print(arr[3, , 2])

print("All elements of the first column across all matrices:")
print(arr[, 1, ])

# Modifying elements
arr[1, 2, 1] <- 20
arr[3, , 2] <- c(21, 22)
print("Modified Array:")
print(arr)

# Array operations
arr1 <- array(1:8, dim = c(2, 2, 2))
arr2 <- array(9:16, dim = c(2, 2, 2))

add_result <- arr1 + arr2
print("Array Addition Result:")
print(add_result)

sub_result <- arr1 - arr2
print("Array Subtraction Result:")
print(sub_result)

mul_result <- arr1 * arr2
print("Element-wise Multiplication Result:")
print(mul_result)

div_result <- arr1 / arr2
print("Element-wise Division Result:")
print(div_result)

# Array functions
print("Dimensions of the Array:")
print(dim(arr))

dimnames(arr) <- list(c("Row1", "Row2", "Row3"), c("Col1", "Col2"), c("Matrix1", "Matrix2"))
print("Array with Dimension Names:")
print(arr)

perm_arr <- aperm(arr, c(2, 1, 3))
print("Permuted Array:")
print(perm_arr)

Conclusion

In this chapter, you learned about arrays in R, including how to create, access, modify, and perform operations on arrays. You also learned about various array functions for handling and manipulating arrays. Arrays are essential data structures for managing multi-dimensional data in R. By mastering arrays, you can efficiently handle complex data structures and perform advanced data analysis in your R programs.

Leave a Comment

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

Scroll to Top