- 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 create a subset of matrix in R using greater than or less than a certain value of a column?
Subsetting can be required in many different ways, we can say that there might be infinite number of ways for subsetting as it depends on the objective of the bigger or smaller analysis. One such way is subsetting a matrix based on a certain value of column of the matrix. In R, we can easily do the same with the help of subset function as shown in below example.
Example
M<-matrix(1:100,ncol=10) M
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99 [10,] 10 20 30 40 50 60 70 80 90 100
Example
subset(M,M[,1]<6)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95
Example
subset(M,M[,1]>6)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 7 17 27 37 47 57 67 77 87 97 [2,] 8 18 28 38 48 58 68 78 88 98 [3,] 9 19 29 39 49 59 69 79 89 99 [4,] 10 20 30 40 50 60 70 80 90 100
Example
subset(M,M[,1]==6)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 6 16 26 36 46 56 66 76 86 96
Example
subset(M,M[,1]>3)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 4 14 24 34 44 54 64 74 84 94 [2,] 5 15 25 35 45 55 65 75 85 95 [3,] 6 16 26 36 46 56 66 76 86 96 [4,] 7 17 27 37 47 57 67 77 87 97 [5,] 8 18 28 38 48 58 68 78 88 98 [6,] 9 19 29 39 49 59 69 79 89 99 [7,] 10 20 30 40 50 60 70 80 90 100
Example
subset(M,M[,1]<8)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97
Example
subset(M,M[,2]<20)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99
Example
subset(M,M[,2]<25)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99 [10,] 10 20 30 40 50 60 70 80 90 100
Example
subset(M,M[,5]>45)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 6 16 26 36 46 56 66 76 86 96 [2,] 7 17 27 37 47 57 67 77 87 97 [3,] 8 18 28 38 48 58 68 78 88 98 [4,] 9 19 29 39 49 59 69 79 89 99 [5,] 10 20 30 40 50 60 70 80 90 100
Example
subset(M,M[,5]>48)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 9 19 29 39 49 59 69 79 89 99 [2,] 10 20 30 40 50 60 70 80 90 100
Example
subset(M,M[,8]>75)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 6 16 26 36 46 56 66 76 86 96 [2,] 7 17 27 37 47 57 67 77 87 97 [3,] 8 18 28 38 48 58 68 78 88 98 [4,] 9 19 29 39 49 59 69 79 89 99 [5,] 10 20 30 40 50 60 70 80 90 100
Example
subset(M,M[,9]>81)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 2 12 22 32 42 52 62 72 82 92 [2,] 3 13 23 33 43 53 63 73 83 93 [3,] 4 14 24 34 44 54 64 74 84 94 [4,] 5 15 25 35 45 55 65 75 85 95 [5,] 6 16 26 36 46 56 66 76 86 96 [6,] 7 17 27 37 47 57 67 77 87 97 [7,] 8 18 28 38 48 58 68 78 88 98 [8,] 9 19 29 39 49 59 69 79 89 99 [9,] 10 20 30 40 50 60 70 80 90 100
Example
subset(M,M[,9]<90)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99
Example
subset(M,M[,10]<100)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99
Example
subset(M,M[,10]<96)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95
Advertisements