How to find the indexes of minimum values in a vector if there are ties in R?

R ProgrammingServer Side ProgrammingProgramming

If we have repeated values in a vector that means we have ties in the vector, therefore, the indexes of values will help us to identify the positions of a particular value in the vector. We can use which function with min function to find the positions of minimum values in a vector, if there exists more than one minimum then the output will show all the relevant positions.

Example

 Live Demo

x1<-sample(0:5,20,replace=TRUE)
x1

Output

[1] 2 3 1 0 0 2 0 0 0 1 1 5 1 1 2 4 1 1 4 0

Example

which(x1==min(x1))

Output

[1] 4 9 18

Example

 Live Demo

x2<-sample(0:10,100,replace=TRUE)
x2

Output

[1] 2 3 7 1 1 9 10 2 9 7 5 1 1 2 8 8 6 3 7 5 2 9 9 5 4
[26] 3 6 10 0 6 6 7 4 7 8 9 1 8 9 6 9 10 6 3 2 5 9 8 9 7
[51] 7 8 8 5 10 5 8 0 9 5 10 3 5 1 5 3 10 9 1 9 0 0 8 6 5
[76] 6 1 5 8 2 2 9 5 5 10 9 7 3 4 3 6 0 2 6 6 7 0 2 1 3

Example

which(x2==min(x2))

Output

[1] 35 49 57 70 79 82

Example

 Live Demo

x3<-sample(0:50,100,replace=TRUE)
x3

Output

[1] 12 32 50 47 14 0 46 12 46 11 46 30 46 4 2 22 26 36 7 5 34 30 32 22 42
[26] 49 7 46 6 49 15 42 38 38 37 25 39 2 16 26 23 3 24 40 35 46 21 11 21 10
[51] 44 1 1 21 49 19 28 48 19 30 33 36 27 40 22 11 14 13 29 7 31 13 3 5 17
[76] 28 37 4 31 8 11 21 36 39 28 18 13 18 50 48 44 36 20 33 48 19 25 43 50 19

Example

which(x3==min(x3))

Output

[1] 7 12 38 92 98

Example

 Live Demo

x4<-sample(11:99,120,replace=TRUE)
x4

Output

[1] 74 16 54 22 40 64 45 14 12 30 89 12 89 26 77 28 65 24 47 87 74 52 92 34 24
[26] 43 51 21 18 97 85 70 75 56 91 92 63 39 88 74 90 97 53 46 89 53 80 51 66 35
[51] 74 75 57 66 21 48 51 98 64 85 82 71 35 89 77 72 69 88 93 79 63 77 75 21 51
[76] 97 99 47 14 49 70 66 19 84 65 41 27 73 15 74 93 55 24 77 13 41 19 97 70 96
[101] 90 98 56 82 52 20 28 22 22 67 74 69 87 64 85 54 71 63 90 13

Example

which(x4==min(x4))

Output

[1] 4

Example

 Live Demo

x5<-sample(101:110,120,replace=TRUE)
x5

Output

[1] 108 105 105 104 102 102 101 103 103 105 103 108 103 106 109 107 102 103
[19] 110 108 103 109 108 107 105 108 109 108 106 106 109 108 106 110 103 101
[37] 108 104 101 107 102 105 102 109 107 106 108 104 105 103 103 107 108 109
[55] 106 110 101 102 105 107 105 109 105 105 102 101 101 103 106 102 104 102
[73] 110 106 108 105 102 108 109 109 101 110 107 101 101 102 107 107 101 108
[91] 101 101 110 101 101 103 105 107 108 110 110 105 108 102 106 103 110 101
[109] 103 101 106 101 106 104 103 109 105 108 103 102

Example

which(x5==min(x5))

Output

[1] 5 10 15 24 25 26 39 40 48 76 83 87 91 116 117

Example

 Live Demo

x6<-rpois(120,2)
x6

Output

[1] 1 0 0 1 3 3 0 0 1 7 1 2 2 1 3 2 0 1 4 6 0 3 2 1 0 1 2 2 3 3 1 3 1 4 3 0 3
[38] 1 1 1 2 3 2 4 4 2 3 3 5 5 3 3 0 2 2 2 2 1 1 4 1 0 0 3 3 2 2 2 0 1 2 1 2 0
[75] 2 0 1 2 2 3 3 0 1 4 1 1 2 1 2 0 4 2 1 4 0 3 0 2 0 6 2 1 3 2 2 2 4 1 1 1 2
[112] 3 2 0 2 0 3 1 1 1

Example

which(x6==min(x6))

Output

[1] 4 21 22 35 36 41 47 68 69 76 78 85 104

Example

 Live Demo

x7<-rpois(120,5)
x7

Output

[1] 5 7 6 0 5 4 1 4 3 5 7 8 5 5 5 7 3 6 7 5 4 4 1 4 4
[26] 8 3 5 8 7 8 6 4 4 2 4 6 6 4 7 8 4 7 5 4 6 8 9 9 6
[51] 3 4 5 5 6 6 7 6 5 7 7 2 6 6 2 10 7 3 6 2 6 5 13 4 5
[76] 8 6 5 6 3 6 6 6 7 1 7 4 7 8 3 6 5 9 3 5 3 4 7 3 7
[101] 9 6 7 2 6 6 6 3 5 4 2 0 4 6 6 6 3 8 6 7

Example

which(x7==min(x7))

Output

[1] 11 51 66 98 112

Example

 Live Demo

x8<-round(runif(120,2,5))
x8

Output

[1] 3 2 3 5 3 4 2 2 4 5 3 4 2 3 2 2 3 5 5 2 3 2 3 5 5 4 2 3 3 4 4 3 2 4 3 3 3
[38] 3 4 3 4 4 4 3 5 4 3 2 3 4 4 4 3 3 2 2 5 3 4 4 5 3 2 2 5 2 2 4 3 3 3 4 2 2
[75] 4 5 4 3 5 3 3 3 2 4 4 3 4 3 3 5 3 5 2 5 3 3 3 2 3 2 4 5 5 4 3 3 2 5 5 2 3
[112] 3 2 2 2 4 3 4 3 3

Example

which(x8==min(x8))

Output

[1] 4 5 24 27 28 29 49 51 52 62 68 71 89 95 116

Example

 Live Demo

x9<-round(rexp(120,1.13))
x9

Output

[1] 1 1 4 2 0 1 4 1 1 0 1 4 1 0 1 1 0 0 0 1 1 1 1 2 3 1 0 1 1 0 1 1 1 1 2 0 1
[38] 2 3 1 0 0 0 1 0 0 1 2 1 1 1 0 1 3 1 4 1 0 2 1 2 0 1 3 2 3 0 0 3 1 0 2 1 4
[75] 0 2 1 0 0 1 1 1 1 0 1 0 2 2 1 4 1 0 1 0 2 1 1 1 4 0 3 2 0 0 1 1 1 0 0 0 1
[112] 1 0 0 0 1 3 0 0 0

Example

which(x9==min(x9))

Output

[1] 1 3 4 5 6 8 9 10 11 16 18 20 23 25 26 27 28 32 34
[20] 39 44 47 49 50 51 53 54 56 57 61 64 67 73 74 84 87 88 92
[39] 95 98 100 101 102 109 110 112 113 114 115 117 118

Example

 Live Demo

x10<-sample(10001:10009,120,replace=TRUE) x10

Output

[1] 10003 10002 10005 10002 10001 10004 10005 10003 10007 10004 10007 10005
[13] 10002 10009 10008 10007 10006 10004 10003 10004 10006 10004 10002 10005
[25] 10005 10007 10009 10005 10005 10008 10005 10004 10007 10007 10008 10004
[37] 10004 10002 10006 10005 10004 10001 10002 10002 10009 10004 10001 10002
[49] 10006 10001 10008 10008 10006 10002 10005 10009 10001 10009 10005 10002
[61] 10007 10006 10007 10005 10005 10003 10002 10007 10003 10005 10008 10001
[73] 10005 10007 10008 10001 10005 10006 10003 10004 10001 10007 10002 10009
[85] 10003 10002 10001 10004 10001 10005 10004 10002 10007 10003 10006 10003
[97] 10009 10009 10005 10005 10002 10005 10003 10007 10003 10008 10002 10009
[109] 10007 10007 10008 10005 10005 10005 10003 10005 10007 10005 10007 10003

Example

which(x10==min(x10))

Output

[1] 12 15 19 23 32 33 40 42 59 83 89 90 95 107 113
raja
Published on 10-Oct-2020 13:15:23
Advertisements