VOOZH about

URL: https://www.geeksforgeeks.org/r-language/randomize-rows-of-a-matrix-in-r/

⇱ Randomize rows of a matrix in R - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Randomize rows of a matrix in R

Last Updated : 23 Jul, 2025

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()'.

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()'.

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()'.

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()'.

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.

Comment
Article Tags:

Explore