Open In App

Randomize rows of a matrix in R

Last Updated : 25 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will examine various methods to randomize rows of a matrix in the R Programming Language.

What is a matrix?

A matrix is a two-dimensional arrangement of data in rows and columns. A matrix can able to contain data of various types such as numeric, characters, and logical values. Inside the matrix, rows are arranged horizontally and columns are arranged vertically. It is possible to access the data in the matrix easily. By using the function ‘matrix()’ matrices are created.

How to randomize the rows in a matrix

R language offers various methods to randomize rows in a matrix. By using these methods provided by R, it is possible to randomize the rows easily. Some of the methods to randomize rows in the matrix are:

By using the sample() Function

These method is used to randomize the rows in a matrix efficiently. The syntax is:

sample(matrix)

In the below example, we created a matrix and randomized the rows in the matrix using ‘sample()’.

R
# Create a sample matrix
mat <- matrix(1:12, nrow = 4, byrow = TRUE)
print("Original matrix:")
print(mat)

# Randomly shuffle the row indices
shuffled_indices <- sample(nrow(mat))

# Reorder the rows of the matrix using the shuffled indices
shuffled_mat <- mat[shuffled_indices, ]
print("Matrix with shuffled rows:")
print(shuffled_mat)

Output:

[1] "Original matrix:"
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12

[1] "Matrix with shuffled rows:"
[,1] [,2] [,3]
[1,] 7 8 9
[2,] 1 2 3
[3,] 10 11 12
[4,] 4 5 6

In the below example, we created a matrix and randomized the rows in the matrix using ‘sample()’.

R
# Creating a matrix
mat <- matrix(rnorm(20), ncol = 2)

print("Original matrix:")
print(mat)

# Randomize rows using sample() function
randomized_mat <- mat[sample(nrow(mat)), ]

print("Randomized rows in the matrix:")
print(randomized_mat)

Output:

[1] "Original matrix is"
[,1] [,2]
[1,] -0.16166696 1.4503207
[2,] -0.13973526 -0.5281976
[3,] 0.07237113 -0.4884292
[4,] 0.57924205 1.6364421
[5,] 0.60452636 1.2891670
[6,] -0.10563316 1.6458899
[7,] 0.82316150 0.8521255
[8,] 0.35773984 -1.1088951
[9,] 0.98341252 -0.8378721
[10,] 1.24741062 1.1456645

[1] "Randomized rows in the matrix is"
[,1] [,2]
[1,] 1.24741062 1.1456645
[2,] 0.57924205 1.6364421
[3,] -0.16166696 1.4503207
[4,] 0.60452636 1.2891670
[5,] 0.35773984 -1.1088951
[6,] 0.82316150 0.8521255
[7,] -0.10563316 1.6458899
[8,] 0.07237113 -0.4884292
[9,] -0.13973526 -0.5281976
[10,] 0.98341252 -0.8378721

By using the sample.int() Function

These method is used to randomize the rows in a matrix efficiently. The syntax is:

sample.int(matrix)

In the below example, we created a matrix and randomized the rows in the matrix using ‘sample.int()’.

R
# Creating a matrix
mat <- matrix(21:40, ncol=4,nrow = 5)

print("Original matrix is")
print(mat)

# Randomize rows using sample.int() function
randomized_mat <- mat[sample.int(nrow(mat)), ]

print("Randomized rows in the matrix is")
print(randomized_mat)

Output:

[1] "Original matrix is"
       [,1] [,2] [,3] [,4]
[1,]   21   26   31   36
[2,]   22   27   32   37
[3,]   23   28   33   38
[4,]   24   29   34   39
[5,]   25   30   35   40

[1] "Randomized rows in the matrix is"
        [,1] [,2] [,3] [,4]
[1,]   24   29   34   39
[2,]   21   26   31   36
[3,]   25   30   35   40
[4,]   22   27   32   37
[5,]   23   28   33   38

In the below example, we created a matrix and randomized the rows in the matrix using ‘sample.int()’.

R
# Creating matrix with characters
mat1 <- matrix(letters[1:25], nrow = 5)
print("Original matrix is")
print(mat1)

# Number of rows and columns in the matrix
n_rows <- nrow(mat1)
n_cols <- ncol(mat1)

indices <- sample.int(n_rows)
res <- mat1[indices, ]

print("Randomised rows in the matrix is")
print(res)

Output:

[1] "Original matrix is"
       [,1]   [,2]  [,3]   [,4]  [,5]
[1,]   "a"   "f"  "k"   "p"   "u" 
[2,]  "b"   "g"  "l"   "q"   "v" 
[3,]  "c"   "h"  "m"  "r"   "w" 
[4,]  "d"   "i"   "n"   "s"   "x" 
[5,]  "e"   "j"   "o"   "t"   "y" 

[1] "Randomised rows in the matrix is"
       [,1]   [,2]  [,3]  [,4]  [,5]
[1,]  "d"  "i"    "n"   "s"   "x" 
[2,]  "e"  "j"    "o"   "t"   "y" 
[3,]  "c"  "h"   "m"  "r"  "w" 
[4,]  "a"  "f"   "k"   "p"  "u" 
[5,]  "b"  "g"   "l"   "q"  "v"

Conclusion

In conclusion, we learned about how to randomize the rows in a matrix by using various methods . R language provides versatile tools while dealing with these matrices.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads