
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Subset Matrix Row Values Based on Different Columns
Suppose we have a matrix called M that contains three columns and we want to subset row values of M but from different columns that means the subset will not have values only from a single column.
Hence, before creating the subset we first need to find the column numbers that can be used for the creation of subset and this can be done with the help of sample function if column numbers are not known. After that we can use cbind function with single square brackets for subsetting.
Check out the Examples given below to understand how it can be done.
Example 1
Following snippet creates a sample matrix −
M1<-matrix(rpois(80,10),ncol=4) M1
The following matrix is created −
[,1] [,2] [,3] [,4] [1,] 7 11 17 14 [3,] 6 5 10 9 [4,] 8 9 14 10 [5,] 10 14 9 6 [6,] 13 6 6 6 [7,] 10 15 10 10 [8,] 9 10 10 9 [9,] 13 9 14 10 [10,] 9 4 6 14 [11,] 12 11 11 6 [12,] 8 15 8 8 [13,] 7 13 11 9 [14,] 16 9 9 12 [15,] 10 9 11 13 [16,] 10 10 6 5 [17,] 11 7 15 13 [18,] 9 6 13 4 [19,] 18 4 14 11 [20,] 17 11 9 7
To subset matrix row values based on different columns in R, on the above created matrix add the following code to the above snippet −
M1<-matrix(rpois(80,10),ncol=4) Columns<-sample(1:4,20,replace=TRUE) Columns
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 2 4 2 1 4 3 2 4 1 4 3 4 4 2 1 4 3 4 1 1
To subset matrix row values based on different columns in R, on the above created matrix add the following code to the above snippet −
M1<-matrix(rpois(80,10),ncol=4) Columns<-sample(1:4,20,replace=TRUE) M1[cbind(1:nrow(M1),Columns)]
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 12 14 5 8 6 6 15 9 13 14 11 8 9 9 10 5 15 4 18 17
Example 2
Following snippet creates a sample matrix −
M2<-matrix(rnorm(40),ncol=2) M2
The following matrix is created −
[,1] [,2] [1,] 0.0526401 -1.33823138 [2,] -0.9658880 -0.19916662 [3,] -0.1729805 0.38128801 [4,] 1.0575809 -1.74814941 [5,] 0.9132299 2.22706167 [6,] -1.0254388 0.72220810 [7,] -0.4295359 0.23052190 [8,] -0.5603532 -1.71916189 [9,] -1.0226156 -0.62879763 [10,] 0.4251926 0.51384615 [11,] 0.9658157 0.11204097 [12,] -1.1791793 0.90926504 [13,] -1.0283043 0.32224749 [14,] -1.0399879 -0.75888477 [15,] 0.2083132 -0.88142437 [16,] 1.1399261 -0.43637839 [17,] 0.3146594 0.84852872 [18,] 0.9208013 0.09130219 [19,] -0.7754234 -0.17243346 [20,] -1.8399159 0.35939999
To subset matrix row values based on different columns in R, on the above created matrix add the following code to the above snippet −
M2<-matrix(rnorm(40),ncol=2) Columns<-sample(1:2,20,replace=TRUE) Columns
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 1 2 1 1 1 2 2 2 2 1 2 2 1 1 2 2 1 2 1 2
To subset matrix row values based on different columns in R, on the above created matrix add the following code to the above snippet −
M2<-matrix(rnorm(40),ncol=2) Columns<-sample(1:2,20,replace=TRUE) M2[cbind(1:nrow(M2),Columns)]
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 0.05264010 -0.19916662 -0.17298052 1.05758086 0.91322994 0.72220810 [7] 0.23052190 -1.71916189 -0.62879763 0.42519260 0.11204097 0.90926504 [13] -1.02830426 -1.03998789 -0.88142437 -0.43637839 0.31465938 0.09130219 [19] -0.77542338 0.35939999
Example 3
Following snippet creates a sample matrix −
M3<-matrix(round(runif(60,1,5),2),ncol=3) M3
The following matrix is created −
[,1] [,2] [,3] [1,] 3.82 2.32 3.00 [2,] 4.07 4.21 2.42 [3,] 1.98 4.75 4.18 [4,] 2.34 3.73 3.60 [5,] 2.63 2.69 4.82 [6,] 2.62 3.76 1.57 [7,] 4.28 2.62 4.12 [8,] 2.86 4.98 4.76 [9,] 2.36 1.61 2.00 [10,] 3.22 1.17 2.97 [11,] 2.04 2.41 3.90 [12,] 1.76 2.29 2.14 [13,] 3.05 1.07 2.43 [14,] 4.98 1.72 1.42 [15,] 3.57 2.90 4.89 [16,] 1.00 4.98 4.58 [17,] 2.09 1.49 1.88 [18,] 2.49 1.67 3.49 [19,] 2.59 1.55 1.21 [20,] 4.43 3.39 2.81
To subset matrix row values based on different columns in R, on the above created matrix add the following code to the above snippet −
M3<-matrix(round(runif(60,1,5),2),ncol=3) Columns<-sample(1:3,20,replace=TRUE) Columns
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 2 2 3 3 3 2 2 2 3 1 1 1 2 2 1 3 1 2 2 3
To subset matrix row values based on different columns in R, on the above created matrix add the following code to the above snippet −
M3<-matrix(round(runif(60,1,5),2),ncol=3) Columns<-sample(1:3,20,replace=TRUE) M3[cbind(1:nrow(M3),Columns)]
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 2.32 4.21 4.18 3.60 4.82 3.76 2.62 4.98 2.00 3.22 2.04 1.76 1.07 1.72 3.57 [16] 4.58 2.09 1.67 1.55 2.81