Arrays, Matrices, and Lists in R
In R, arrays, matrices, and lists are important data structures that allow you to organize and manipulate data in various ways. Understanding these data structures is essential for handling multi-dimensional data and more complex analytical tasks.
An array in R is a multi-dimensional collection of elements, all of which must be of the same type (e.g., numeric, character, logical). Arrays can have more than two dimensions, unlike matrices which are always two-dimensional.
Creating an Array:
You can create an array using the array() function, specifying the dimensions of the array.
Example:
R
# Creating a 3-dimensional array
my_array <- array(1:24, dim = c(4, 3, 2)) # 4 rows, 3 columns, 2 layers
print(my_array)
dim: A vector specifying the dimensions of the array (number of rows, columns, and layers).
The array is filled column-wise by default.
Accessing Elements in an Array:
You can access specific elements by using the array index notation, where multiple indices are used for multi-dimensional data.
R
# Accessing an element in the array (2nd row, 3rd column, 1st layer)
my_array[2, 3, 1]
Array Operations:
Arrays support element-wise operations like addition, subtraction, multiplication, etc.
R
# Adding 10 to each element of the array
my_array + 10
A matrix is a two-dimensional array in which elements are arranged in rows and columns. All elements in a matrix must be of the same type.
Creating a Matrix:
You can create a matrix using the matrix() function by specifying the data and the number of rows or columns.
Example:
R
# Creating a matrix with 2 rows and 3 columns
my_matrix <- matrix(1:6, nrow = 2, ncol = 3)
print(my_matrix)
nrow: Number of rows.
ncol: Number of columns.
By default, the matrix fills its elements column-wise.
Accessing Elements in a Matrix:
You can access individual elements using the row and column indices.
R
# Accessing element in 1st row, 2nd column
my_matrix[1, 2]
Matrix Operations:
Matrices in R support operations like addition, multiplication, transposition, etc.
Addition:
R
matrix1 <- matrix(1:6, nrow = 2)
matrix2 <- matrix(7:12, nrow = 2)
matrix_sum <- matrix1 + matrix2 # Element-wise addition
print(matrix_sum)
Multiplication (Element-wise):
R
matrix_prod <- matrix1 * matrix2 # Element-wise multiplication
print(matrix_prod)
Matrix Multiplication (Dot product):
R
matrix_mult <- matrix1 %*% matrix2 # Matrix multiplication
print(matrix_mult)
Transpose:
R
Copy code
matrix_transpose <- t(my_matrix) # Transpose of the matrix
print(matrix_transpose)
A list in R is a collection of elements that can each be of different types (numeric, character, logical, or even other lists). Lists are highly flexible and useful when dealing with complex data structures that mix different types of information.
Creating a List:
You can create a list using the list() function.
Example:
R
# Creating a list with different types of elements
my_list <- list(Name = "John", Age = 25, Scores = c(80, 90, 85), Married = FALSE)
print(my_list)
In this example, the list contains:
A character element ("John").
A numeric element (25).
A vector (c(80, 90, 85)).
A logical value (FALSE).
Accessing Elements in a List:
You can access list elements using either indexing or named indexing.
By Index (Position):
R
my_list[[1]] # Accesses the 1st element ("John")
By Name (If the list has named elements):
R
my_list$Age # Accesses the "Age" element (25)
Accessing Elements of a Sublist:
R
my_list[[3]][2] # Accesses the 2nd element of the "Scores" vector (90)
List Operations:
Adding Elements: You can add elements to a list by assigning them with a new name or index.
R
Copy code
my_list$City <- "New York" # Adds a new element "City"
List Length: You can get the length (number of elements) of the list.
R
Copy code
length(my_list) # Returns the number of elements in the list
Modifying Elements: You can modify an element in the list using indexing or naming.
R
Copy code
my_list$Age <- 26 # Updates the Age element to 26