Intoduction to Arrays

R – Array

Arrays are fundamental data storage structures defined with a specific number of dimensions. They are used to allocate space in contiguous memory locations.

In R Programming, one-dimensional arrays are called vectors, where their single dimension is their length. Two-dimensional arrays are referred to as matrices, which consist of a defined number of rows and columns. Arrays in R hold elements of the same data type. Vectors serve as inputs to create arrays, specifying their dimensions.

Creating an Array

In R, arrays can be created using the array() function. The function takes a list of elements and dimensions as inputs to create the desired array.

Syntax:

array(data, dim = c(nrow, ncol, nmat), dimnames = names)

Components:

  • nrow: Number of rows.
  • ncol: Number of columns.
  • nmat: Number of matrices with dimensions nrow * ncol.
  • dimnames: Defaults to NULL. Alternatively, a list can be provided containing names for each component of the array dimensions.
Uni-Dimensional Array

A vector, a one-dimensional array, has its length as its dimension. It can be created using the c() function.

Example:

vec <- c(10, 20, 30, 40, 50)
print(vec)

# Displaying the length of the vector
cat("Length of the vector: ", length(vec))

Output:

[1] 10 20 30 40 50
Length of the vector:  5
Multi-Dimensional Array

A matrix, or a two-dimensional array, is defined by rows and columns of the same data type. Matrices are created using the array() function.

Example:

# Create a matrix with values from 15 to 26
mat <- array(15:26, dim = c(2, 3, 2))
print(mat)

Output:

, , 1
     [,1] [,2] [,3]
[1,]   15   17   19
[2,]   16   18   20

, , 2
     [,1] [,2] [,3]
[1,]   21   23   25
[2,]   22   24   26
Naming Array Dimensions

You can assign names to rows, columns, and matrices using vectors for better readability.

Example:

rows <- c("Row1", "Row2")
columns <- c("Col1", "Col2", "Col3")
matrices <- c("Matrix1", "Matrix2")

named_array <- array(1:12, dim = c(2, 3, 2),
                     dimnames = list(rows, columns, matrices))
print(named_array)

Output:

, , Matrix1
     Col1 Col2 Col3
Row1    1    3    5
Row2    2    4    6

, , Matrix2
     Col1 Col2 Col3
Row1    7    9   11
Row2    8   10   12
Accessing Arrays

You can access elements of arrays using indices for each dimension. Names or positions can be used.

Example:

vec <- c(5, 10, 15, 20, 25)
cat("Vector:", vec)
cat("Second element:", vec[2])

Output:

Vector: 5 10 15 20 25
Second element: 10
Accessing Matrices in an Array

Example:

rows <- c("A", "B")
columns <- c("X", "Y", "Z")
matrices <- c("M1", "M2")

multi_array <- array(1:12, dim = c(2, 3, 2),
                     dimnames = list(rows, columns, matrices))

# Accessing first matrix
print("Matrix M1")
print(multi_array[, , "M1"])

# Accessing second matrix by index
print("Matrix 2")
print(multi_array[, , 2])

Output:

Matrix M1
     X Y Z
A    1 3 5
B    2 4 6

Matrix 2
     X Y Z
A    7 9 11
B    8 10 12
Accessing Specific Rows and Columns

Example:

print("First row of Matrix 1")
print(multi_array[1, , "M1"])

print("Second column of Matrix 2")
print(multi_array[, 2, 2])

Output:

First row of Matrix 1
X Y Z
1 3 5

Second column of Matrix 2
A 9
B 10
Modifying Arrays

Adding Elements to Arrays: New elements can be added at specific positions or appended to the array.

Example:

vec <- c(1, 2, 3, 4)

# Adding an element using c()
vec <- c(vec, 5)
print("After appending an element:")
print(vec)

# Using append() to add after the 2nd element
vec <- append(vec, 10, after = 2)
print("After using append:")
print(vec)

Output:

After appending an element:
[1] 1 2 3 4 5

After using append:
[1]  1  2 10  3  4  5

Removing Elements

Elements can be removed using logical conditions or indices.

Example:

vec <- c(1, 2, 3, 4, 5, 6)
vec <- vec[vec != 4]  # Removing element with value 4
print(vec)

Output:

[1] 1 2 3 5 6

Comments

Leave a Reply

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