- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to randomize rows of a matrix in R?
To randomize rows of a matrix in R, we can use sample function along with nrow function to get the random rows and then subset the matrix with single square brackets.
For example, if we have a matrix called M then randomization of rows in M can be done by using the command given below −
Random_rows<-sample(nrow(M)) and M[Random_rows,]
Example 1
Following snippet creates a matrix −
M1<-matrix(rnorm(40),ncol=2) M1
The following matrix is created −
[,1] [,2] [1,] -0.1519466 2.3807947 [2,] 0.2484261 0.4204072 [3,] -0.7457339 -3.1329862 [4,] 0.7760234 -0.2282981 [5,] -0.8466272 -1.4497825 [6,] 2.3825231 -1.5571171 [7,] 0.2485765 -0.4825909 [8,] 0.6356381 -0.6498445 [9,] -0.5413304 1.1162191 [10,] -0.7497836 -0.1187549 [11,] 0.9855236 0.6309653 [12,] 0.9829068 -0.7666931 [13,] -0.2415028 0.9425981 [14,] 0.3799880 1.0948588 [15,] -1.1123904 -0.2507432 [16,] -1.6995406 0.9193191 [17,] -0.7319311 -0.4822913 [18,] -0.2378776 0.4848352 [19,] 1.1436393 0.9658798 [20,] 0.3911035 -0.2250289
To create a vector of random rows in M1, add the following code to the above snippet −
M1<-matrix(rnorm(40),ncol=2) Random_rows_1<-sample(nrow(M1)) Random_rows_1
Output
If you execute all the above given snippets as a single program, it generates the following output −
[1] 10 20 18 19 14 16 13 1 12 9 3 6 5 11 4 17 2 15 7 8
To randomise rows of M1, add the following code to the above snippet −
M1[Random_rows_1,]
Output
If you execute all the above given snippets as a single program, it generates the following output −
[,1] [,2] [1,] -0.7497836 -0.1187549 [2,] 0.3911035 -0.2250289 [3,] -0.2378776 0.4848352 [4,] 1.1436393 0.9658798 [5,] 0.3799880 1.0948588 [6,] -1.6995406 0.9193191 [7,] -0.2415028 0.9425981 [8,] -0.1519466 2.3807947 [9,] 0.9829068 -0.7666931 [10,] -0.5413304 1.1162191 [11,] -0.7457339 -3.1329862 [12,] 2.3825231 -1.5571171 [13,] -0.8466272 -1.4497825 [14,] 0.9855236 0.6309653 [15,] 0.7760234 -0.2282981 [16,] -0.7319311 -0.4822913 [17,] 0.2484261 0.4204072 [18,] -1.1123904 -0.2507432 [19,] 0.2485765 -0.4825909 [20,] 0.6356381 -0.6498445
Example 2
Following snippet creates a matrix −
M2<-matrix(rpois(80,5),ncol=4) M2
The following matrix is created −
[,1][,2][,3][,4] [1,] 4 10 7 2 [2,] 7 5 6 2 [3,] 3 6 5 4 [4,] 5 5 4 8 [5,] 3 6 9 3 [6,] 6 5 10 9 [7,] 4 3 5 2 [8,] 5 3 9 4 [9,] 5 4 6 3 [10,] 4 1 2 3 [11,] 8 4 2 4 [12,] 3 4 3 8 [13,] 10 6 3 6 [14,] 9 8 8 2 [15,] 1 2 2 5 [16,] 3 4 5 3 [17,] 6 4 5 4 [18,] 6 2 3 5 [19,] 4 6 6 3 [20,] 6 5 4 4
To create a vector of random rows in M2, add the following code to the above snippet −
M2<-matrix(rpois(80,5),ncol=4) Random_rows_2<-sample(nrow(M2)) Random_rows_2
Output
If you execute all the above given snippets as a single program, it generates the following output −
[1] 7 20 4 19 1 11 16 9 3 17 8 2 18 15 13 14 6 5 12 10
To randomise rows of M2, add the following code to the above snippet −
M2[Random_rows_2,]
Output
If you execute all the above given snippets as a single program, it generates the following output −
[,1][,2][,3][,4] [1,] 4 3 5 2 [2,] 6 5 4 4 [3,] 5 5 4 8 [4,] 4 6 6 3 [5,] 4 10 7 2 [6,] 8 4 2 4 [7,] 3 4 5 3 [8,] 5 4 6 3 [9,] 3 6 5 4 [10,] 6 4 5 4 [11,] 5 3 9 4 [12,] 7 5 6 2 [13,] 6 2 3 5 [14,] 1 2 2 5 [15,] 10 6 3 6 [16,] 9 8 8 2 [17,] 6 5 10 9 [18,] 3 6 9 3 [19,] 3 4 3 8 [20,] 4 1 2 3
- Related Articles
- How to replicate a matrix by rows in R?
- How to combine matrix rows alternately in R?
- How to convert matrix rows into a list in R?
- How to create a matrix with equal rows in R?
- How to multiply a matrix columns and rows with the same matrix rows and columns in R?
- How to multiple a matrix rows in R with a vector?
- How to create a subset of rows or columns of a matrix in R?
- How to divide matrix rows by number of columns in R?
- How to divide matrix rows in R by row median?
- How to combine two rows in R matrix by addition?
- How to remove rows that contain NAs in R matrix?
- How to expand a matrix rows by their index position in R?
- How to remove rows in R matrix that contains a specific number?
- How to find the sum of rows, columns, and total in a matrix in R?
- How to randomize an already created vector in R?
