Open In App

Split a Matrix into a List of its Rows using R

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to split a given matrix into a List of its rows using R Programming Language. A matrix in R is a two-dimensional data set with columns and rows that can hold homogeneous data. Splitting a matrix can be useful for various purposes in data manipulation and analysis.

There are three types of methods for Split a matrix into a list of its rows

  • Using for loop
  • Using split() function
  • Using apply() function two-dimensional

Split a matrix into a list of its rows using for loop

Using a for loop we split the matrix into its rows. Splitting a matrix involves iterating through each row index, extracting a row by using indexing then storing it as an element in a list. We will repeat this process for each row of a matrix. at the end matrix will have resulted in a list where each element represents one row of the matrix.

R




# Create a sample matrix
Matrix <- matrix(1:12, nrow = 4)
print('Original Matrix is: ')
print(Matrix)
# Initialize an empty list to store rows
rows_list <- list()
 
# Iterate over each row of the matrix
for (i in 1:nrow(Matrix)) {
  # Extract each row and add it to the list
  rows_list[[i]] <- Matrix[i, , drop = FALSE]
}
 
# Print the list of rows
print('Lists of matrix rows after splitting: ')
print(rows_list)


Output:

[1] "Original Matrix is: "
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
[1] "Lists of matrix rows after splitting: "
[[1]]
[,1] [,2] [,3]
[1,] 1 5 9
[[2]]
[,1] [,2] [,3]
[1,] 2 6 10
[[3]]
[,1] [,2] [,3]
[1,] 3 7 11
[[4]]
[,1] [,2] [,3]
[1,] 4 8 12

Split a matrix into a list of its rows using split() function

In this example to split a given matrix into a list of rows in R we use the split() function.

R




# Create a sample matrix
mat <- matrix(101:112, ncol = 3)
print("Original matrix is: ")
print(mat)
 
# Split the matrix into a list of its rows
row_list <- split(mat, row(mat))
 
# Print the resulting list
print(row_list)


Output:

[1] "Original matrix is: "
[,1] [,2] [,3]
[1,] 101 105 109
[2,] 102 106 110
[3,] 103 107 111
[4,] 104 108 112
$`1`
[1] 101 105 109
$`2`
[1] 102 106 110
$`3`
[1] 103 107 111
$`4`
[1] 104 108 112

In below code split() function is used to divide the matrix “mat” into groups based on the row indices. row(mat) generates a vector containing the row numbers of the matrix mat. Here matrix mat has 3 rows so row(mat) will be a vector from 1 to 3. Then split() function creates a list where each element corresponds to a row of the matrix.

Split a matrix into a list of its rows using apply() function

in this approach we will use the apply() function in R which is used to apply a function over the margins of an array, such as rows or columns. In the below example we use apply() function to apply list function to each row of the matrix. Here the list function will converts the each matrix row into a list and apply function will aggregates these lists into a single list, where each element corresponds to a row of the matrix.

R




# Create a matrix of characters
mat <- matrix(c("a", "b", "c",
                "d", "e", "f",
                "g", "h", "i",
                "j", "k", "l"),
              nrow = 4, byrow = TRUE)
print('original matrix: ')
print(mat)
# Using apply() to split the matrix into a list of rows
list_of_rows <- apply(mat, 1, list)
 
# Print the list of rows
print('List of rows: ')
print(list_of_rows)


Output:

[1] "original matrix: "
[,1] [,2] [,3]
[1,] "a" "b" "c"
[2,] "d" "e" "f"
[3,] "g" "h" "i"
[4,] "j" "k" "l"
[1] "List of rows: "
[[1]]
[[1]][[1]]
[1] "a" "b" "c"
[[2]]
[[2]][[1]]
[1] "d" "e" "f"
[[3]]
[[3]][[1]]
[1] "g" "h" "i"
[[4]]
[[4]][[1]]
[1] "j" "k" "l"

Conclusion

Splitting a matrix into a list of its rows in R provides a flexible and useful way to work with individual rows of the matrix. This can be particularly handy when dealing with datasets where rows represent individual observations, and we want to apply operations or analyses to each row separately.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads