R Matrices

A matrix is a two-dimensional data structure that can store elements of the same data type arranged in rows and columns.
Created using the matrix() function.

Example
# Creating a matrix with elements
mat1 <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3, byrow = TRUE)
mat1

# Creating a matrix with a sequence
mat2 <- matrix(1:9, nrow = 3, ncol = 3)
mat2

# Creating a matrix of an collection of items
mat3 <- matrix(c("basket", "board", "tub", "glass"), nrow = 2, ncol = 2)
mat3

Accessing and Checking Elements

We can access the list with index number inside a [column_name, row_name] bracket.
And to check elements we use %in% operator.

Example
mat <- matrix(c("basket", "board", "tub", "glass"), nrow = 2, ncol = 2)
# Accessing elements by index
element <- mat[1, 2]  # Element in the first row, second column

# Accessing entire row or column
first_row <- mat[1, ]
second_column <- mat[, 2]

"basket" %in% mat

Adding and Removing of Rows and Coloums

We use cbind() and rbind() functions to add a rows and coloums respectively.
We use c() functions to remove rows and coloums in a matrix.

  1. cbind() Function - Combines a matrix coloum-wise
  2. rbind() Function - Combines a matrix row-wise
Example
vec1 <- c(1, 2, 3)
vec2 <- c("A", "B", "C")
combined_coloum_matrix <- cbind(vec1, vec2)
combined_coloum_matrix
combined_row_matrix <- rbind(vec1, vec2)
combined_row_matrix

new_matrix <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2, byrow = TRUE)

remove_matrix <- new_matrix[-c(1), -c(1)]
remove_matrix

Matrix Operations and Functions

Functions are a built in methods that performs operations fast and easy.

Operation

Syntax

Description

Example

Addition

mat1 + mat2

Element-wise addition of two matrices.

mat_sum <- mat1 + mat2

Subraction

mat1 - mat2

Element-wise subraction of two matrices.

mat_subract <- mat1 - mat2

Multiplication

mat1 %*% mat2

Matrix multiplication.

mat_mult <- mat1 %*% t(mat2)

Element-wise Operations

mat1^2

Element-wise operations (e.g., square each element).

mat_squared <- mat1^2

Dimensions of a Matrix

dim(mat)

Returns the number of rows and columns in a matrix.

dimensions <- dim(mat)

Length of a Matrix

length(mat)

Returns the length of a matrix.

length_matrix <- length(mat)

Transposing a Matrix

t(mat)

Transposes the rows and columns of a matrix.

transposed_mat <- t(mat)

Sum of Elements in a Matrix

sum(mat)

Calculates the sum of all elements in a matrix.

mat_sum_all <- sum(mat)

Mean of Elements in a Matrix

mean(mat)

Calculates the mean (average) of elements in a matrix.

mat_mean <- mean(mat)


Example
my_list <- list(7, "eight", TRUE)
my_list

length(my_list)

element_exists <- "new_element" %in% names(my_list)
element_exists

df <- as.data.frame(my_list)
df

Quick Recap - Topics Covered

R Matrices

Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.


Example 1
Example 1 Example 2 Example 3 Example 4 Example 5


Quiz


FEEDBACK