- 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 find the index of an element in a matrix column based on some condition in R?
We might want to find the position of a value in a matrix column which is less than a certain value. This will help us to identify the position of critical or threshold values in each column. For example, if we have a matrix M that contains 5 rows and 5 columns with vales in the range of 1 to 100 then we might want to find the index of values in each column that are less than 50 so that we can understand how many columns have such type of values. In R, we can easily do this by using apply function.
Example
Consider the below matrix −
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
apply(M>1,2,which.max)
Output
[1] 2 1 1 1 1 1 1 1 1 1
Example
apply(M>2,2,which.max)
Output
[1] 3 1 1 1 1 1 1 1 1 1
Example
apply(M>5,2,which.max)
Output
[1] 6 1 1 1 1 1 1 1 1 1
Example
apply(M>9,2,which.max)
Output
[1] 10 1 1 1 1 1 1 1 1 1
Example
apply(M<9,2,which.max)
Output
[1] 1 1 1 1 1 1 1 1 1 1
Example
apply(M<5,2,which.max)
Output
[1] 1 1 1 1 1 1 1 1 1 1
Example
apply(M<2,2,which.max)
Output
[1] 1 1 1 1 1 1 1 1 1 1
Example
apply(M>12,2,which.max)
Output
[1] 1 3 1 1 1 1 1 1 1 1
Example
apply(M>22,2,which.max)
Output
[1] 1 1 3 1 1 1 1 1 1 1
Example
apply(M>25,2,which.max)
Output
[1] 1 1 6 1 1 1 1 1 1 1
Example
apply(M>18,2,which.max)
Output
[1] 1 9 1 1 1 1 1 1 1 1
Example
apply(M>48,2,which.max)
Output
[1] 1 1 1 1 9 1 1 1 1 1
Example
apply(M>34,2,which.max)
Output
[1] 1 1 1 5 1 1 1 1 1 1
Example
apply(M>37,2,which.max)
Output
[1] 1 1 1 8 1 1 1 1 1 1
Example
apply(M>39,2,which.max)
Output
[1] 1 1 1 10 1 1 1 1 1 1
Example
apply(M>41,2,which.max)
Output
[1] 1 1 1 1 2 1 1 1 1 1
Example
apply(M>46,2,which.max)
Output
[1] 1 1 1 1 7 1 1 1 1 1
Example
apply(M>51,2,which.max)
Output
[1] 1 1 1 1 1 2 1 1 1 1
Example
apply(M>55,2,which.max)
Output
[1] 1 1 1 1 1 6 1 1 1 1
Example
apply(M>59,2,which.max)
Output
[1] 1 1 1 1 1 10 1 1 1 1
Example
apply(M>69,2,which.max)
Output
[1] 1 1 1 1 1 1 10 1 1 1
Example
apply(M>64,2,which.max)
Output
[1] 1 1 1 1 1 1 5 1 1 1
Example
apply(M>61,2,which.max)
Output
[1] 1 1 1 1 1 1 2 1 1 1
Example
apply(M>72,2,which.max)
Output
[1] 1 1 1 1 1 1 1 3 1 1
Example
apply(M>78,2,which.max)
Output
[1] 1 1 1 1 1 1 1 9 1 1
Example
apply(M>81,2,which.max)
Output
[1] 1 1 1 1 1 1 1 1 2 1
Example
apply(M>87,2,which.max)
Output
[1] 1 1 1 1 1 1 1 1 8 1
Example
apply(M>91,2,which.max)
Output
[1] 1 1 1 1 1 1 1 1 1 2
Example
apply(M>94,2,which.max)
Output
[1] 1 1 1 1 1 1 1 1 1 5
Example
apply(M>99,2,which.max)
Output
[1] 1 1 1 1 1 1 1 1 1 10
Advertisements