Return index of the smallest value in a vector?












52















a <- c(1, 2, 0, 3, 7)


I am looking for a function to return the index of the smallest value, 3. What is it?










share|improve this question





























    52















    a <- c(1, 2, 0, 3, 7)


    I am looking for a function to return the index of the smallest value, 3. What is it?










    share|improve this question



























      52












      52








      52


      10






      a <- c(1, 2, 0, 3, 7)


      I am looking for a function to return the index of the smallest value, 3. What is it?










      share|improve this question
















      a <- c(1, 2, 0, 3, 7)


      I am looking for a function to return the index of the smallest value, 3. What is it?







      r






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 25 '17 at 18:09









      Rich Scriven

      76k899168




      76k899168










      asked Feb 22 '12 at 7:33









      hhhhhh

      20.5k44124213




      20.5k44124213
























          3 Answers
          3






          active

          oldest

          votes


















          80














          You're looking for which.min():



          a <- c(1,2,0,3,7,0,0,0)
          which.min(a)
          # [1] 3

          which(a == min(a))
          # [1] 3 6 7 8


          (As you can see from the above, when several elements are tied for the minimum, which.min() only returns the index of the first one. You can use the second construct if you instead want the indices of all elements that match the minimum value.)






          share|improve this answer


























          • ...yes I was wondering how can I get the indices of all minimum elements? I need to find out how many are the minimum, perfect! Got me some time to understand this, thanks.

            – hhh
            Feb 22 '12 at 7:43








          • 1





            @hhh -- To find out how many elements are the minimum, you can just use: sum(a == min(a)).

            – Josh O'Brien
            Feb 22 '12 at 7:45





















          12














          As an alternative to Josh's answer



          a <- c(1, 2, 0, 3, 7)
          which(a == min(a))


          this gives every index that is equal to the minimum value. So if we had more than one value matching the lowest value



          a <- c(1, 2, 0, 3, 7, 0)
          which(a == min(a)) # returns both 3 and 6
          which.min(a) # returns just 3


          Edit: If what you're looking for is just how many elements are equal to the minimum (as you imply in one of the comments) you can do this instead:



          a <- c(1, 2, 0, 3, 7, 0)
          sum(a == min(a))





          share|improve this answer

































            1














            If you are a fan of efficiency, then you can use the function min_max from the Rfast package, with index = True



            It will return the index of the minimum and the index of the maximum
            value, simultaneously, faster than what has been disused so far.



            E.g.



            a = runif(10000)
            Rfast::min_max(a,index=T)

            # min max
            # 2984 2885

            which(a == min(a))

            #[1] 2984

            a = runif(1000000)
            microbenchmark::microbenchmark(
            min_max = Rfast::min_max(a,index=T),
            which1 = which(a == min(a)),
            which2 = which.min(a)
            )

            Unit: milliseconds
            expr min lq mean median uq max neval
            min_max 1.889293 1.9123860 2.08242647 1.9271395 2.0359730 3.527565 100
            which1 9.809527 10.0342505 13.16711078 10.3671640 14.7839955 111.424664 100
            which2 2.400745 2.4216995 2.66374110 2.4471435 2.5985265 4.259249 100





            share|improve this answer






















              protected by Sotos Aug 19 '17 at 14:47



              Thank you for your interest in this question.
              Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



              Would you like to answer one of these unanswered questions instead?














              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              80














              You're looking for which.min():



              a <- c(1,2,0,3,7,0,0,0)
              which.min(a)
              # [1] 3

              which(a == min(a))
              # [1] 3 6 7 8


              (As you can see from the above, when several elements are tied for the minimum, which.min() only returns the index of the first one. You can use the second construct if you instead want the indices of all elements that match the minimum value.)






              share|improve this answer


























              • ...yes I was wondering how can I get the indices of all minimum elements? I need to find out how many are the minimum, perfect! Got me some time to understand this, thanks.

                – hhh
                Feb 22 '12 at 7:43








              • 1





                @hhh -- To find out how many elements are the minimum, you can just use: sum(a == min(a)).

                – Josh O'Brien
                Feb 22 '12 at 7:45


















              80














              You're looking for which.min():



              a <- c(1,2,0,3,7,0,0,0)
              which.min(a)
              # [1] 3

              which(a == min(a))
              # [1] 3 6 7 8


              (As you can see from the above, when several elements are tied for the minimum, which.min() only returns the index of the first one. You can use the second construct if you instead want the indices of all elements that match the minimum value.)






              share|improve this answer


























              • ...yes I was wondering how can I get the indices of all minimum elements? I need to find out how many are the minimum, perfect! Got me some time to understand this, thanks.

                – hhh
                Feb 22 '12 at 7:43








              • 1





                @hhh -- To find out how many elements are the minimum, you can just use: sum(a == min(a)).

                – Josh O'Brien
                Feb 22 '12 at 7:45
















              80












              80








              80







              You're looking for which.min():



              a <- c(1,2,0,3,7,0,0,0)
              which.min(a)
              # [1] 3

              which(a == min(a))
              # [1] 3 6 7 8


              (As you can see from the above, when several elements are tied for the minimum, which.min() only returns the index of the first one. You can use the second construct if you instead want the indices of all elements that match the minimum value.)






              share|improve this answer















              You're looking for which.min():



              a <- c(1,2,0,3,7,0,0,0)
              which.min(a)
              # [1] 3

              which(a == min(a))
              # [1] 3 6 7 8


              (As you can see from the above, when several elements are tied for the minimum, which.min() only returns the index of the first one. You can use the second construct if you instead want the indices of all elements that match the minimum value.)







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 22 '12 at 7:48

























              answered Feb 22 '12 at 7:36









              Josh O'BrienJosh O'Brien

              128k17275384




              128k17275384













              • ...yes I was wondering how can I get the indices of all minimum elements? I need to find out how many are the minimum, perfect! Got me some time to understand this, thanks.

                – hhh
                Feb 22 '12 at 7:43








              • 1





                @hhh -- To find out how many elements are the minimum, you can just use: sum(a == min(a)).

                – Josh O'Brien
                Feb 22 '12 at 7:45





















              • ...yes I was wondering how can I get the indices of all minimum elements? I need to find out how many are the minimum, perfect! Got me some time to understand this, thanks.

                – hhh
                Feb 22 '12 at 7:43








              • 1





                @hhh -- To find out how many elements are the minimum, you can just use: sum(a == min(a)).

                – Josh O'Brien
                Feb 22 '12 at 7:45



















              ...yes I was wondering how can I get the indices of all minimum elements? I need to find out how many are the minimum, perfect! Got me some time to understand this, thanks.

              – hhh
              Feb 22 '12 at 7:43







              ...yes I was wondering how can I get the indices of all minimum elements? I need to find out how many are the minimum, perfect! Got me some time to understand this, thanks.

              – hhh
              Feb 22 '12 at 7:43






              1




              1





              @hhh -- To find out how many elements are the minimum, you can just use: sum(a == min(a)).

              – Josh O'Brien
              Feb 22 '12 at 7:45







              @hhh -- To find out how many elements are the minimum, you can just use: sum(a == min(a)).

              – Josh O'Brien
              Feb 22 '12 at 7:45















              12














              As an alternative to Josh's answer



              a <- c(1, 2, 0, 3, 7)
              which(a == min(a))


              this gives every index that is equal to the minimum value. So if we had more than one value matching the lowest value



              a <- c(1, 2, 0, 3, 7, 0)
              which(a == min(a)) # returns both 3 and 6
              which.min(a) # returns just 3


              Edit: If what you're looking for is just how many elements are equal to the minimum (as you imply in one of the comments) you can do this instead:



              a <- c(1, 2, 0, 3, 7, 0)
              sum(a == min(a))





              share|improve this answer






























                12














                As an alternative to Josh's answer



                a <- c(1, 2, 0, 3, 7)
                which(a == min(a))


                this gives every index that is equal to the minimum value. So if we had more than one value matching the lowest value



                a <- c(1, 2, 0, 3, 7, 0)
                which(a == min(a)) # returns both 3 and 6
                which.min(a) # returns just 3


                Edit: If what you're looking for is just how many elements are equal to the minimum (as you imply in one of the comments) you can do this instead:



                a <- c(1, 2, 0, 3, 7, 0)
                sum(a == min(a))





                share|improve this answer




























                  12












                  12








                  12







                  As an alternative to Josh's answer



                  a <- c(1, 2, 0, 3, 7)
                  which(a == min(a))


                  this gives every index that is equal to the minimum value. So if we had more than one value matching the lowest value



                  a <- c(1, 2, 0, 3, 7, 0)
                  which(a == min(a)) # returns both 3 and 6
                  which.min(a) # returns just 3


                  Edit: If what you're looking for is just how many elements are equal to the minimum (as you imply in one of the comments) you can do this instead:



                  a <- c(1, 2, 0, 3, 7, 0)
                  sum(a == min(a))





                  share|improve this answer















                  As an alternative to Josh's answer



                  a <- c(1, 2, 0, 3, 7)
                  which(a == min(a))


                  this gives every index that is equal to the minimum value. So if we had more than one value matching the lowest value



                  a <- c(1, 2, 0, 3, 7, 0)
                  which(a == min(a)) # returns both 3 and 6
                  which.min(a) # returns just 3


                  Edit: If what you're looking for is just how many elements are equal to the minimum (as you imply in one of the comments) you can do this instead:



                  a <- c(1, 2, 0, 3, 7, 0)
                  sum(a == min(a))






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 22 '12 at 7:45

























                  answered Feb 22 '12 at 7:39









                  DasonDason

                  44.8k695123




                  44.8k695123























                      1














                      If you are a fan of efficiency, then you can use the function min_max from the Rfast package, with index = True



                      It will return the index of the minimum and the index of the maximum
                      value, simultaneously, faster than what has been disused so far.



                      E.g.



                      a = runif(10000)
                      Rfast::min_max(a,index=T)

                      # min max
                      # 2984 2885

                      which(a == min(a))

                      #[1] 2984

                      a = runif(1000000)
                      microbenchmark::microbenchmark(
                      min_max = Rfast::min_max(a,index=T),
                      which1 = which(a == min(a)),
                      which2 = which.min(a)
                      )

                      Unit: milliseconds
                      expr min lq mean median uq max neval
                      min_max 1.889293 1.9123860 2.08242647 1.9271395 2.0359730 3.527565 100
                      which1 9.809527 10.0342505 13.16711078 10.3671640 14.7839955 111.424664 100
                      which2 2.400745 2.4216995 2.66374110 2.4471435 2.5985265 4.259249 100





                      share|improve this answer




























                        1














                        If you are a fan of efficiency, then you can use the function min_max from the Rfast package, with index = True



                        It will return the index of the minimum and the index of the maximum
                        value, simultaneously, faster than what has been disused so far.



                        E.g.



                        a = runif(10000)
                        Rfast::min_max(a,index=T)

                        # min max
                        # 2984 2885

                        which(a == min(a))

                        #[1] 2984

                        a = runif(1000000)
                        microbenchmark::microbenchmark(
                        min_max = Rfast::min_max(a,index=T),
                        which1 = which(a == min(a)),
                        which2 = which.min(a)
                        )

                        Unit: milliseconds
                        expr min lq mean median uq max neval
                        min_max 1.889293 1.9123860 2.08242647 1.9271395 2.0359730 3.527565 100
                        which1 9.809527 10.0342505 13.16711078 10.3671640 14.7839955 111.424664 100
                        which2 2.400745 2.4216995 2.66374110 2.4471435 2.5985265 4.259249 100





                        share|improve this answer


























                          1












                          1








                          1







                          If you are a fan of efficiency, then you can use the function min_max from the Rfast package, with index = True



                          It will return the index of the minimum and the index of the maximum
                          value, simultaneously, faster than what has been disused so far.



                          E.g.



                          a = runif(10000)
                          Rfast::min_max(a,index=T)

                          # min max
                          # 2984 2885

                          which(a == min(a))

                          #[1] 2984

                          a = runif(1000000)
                          microbenchmark::microbenchmark(
                          min_max = Rfast::min_max(a,index=T),
                          which1 = which(a == min(a)),
                          which2 = which.min(a)
                          )

                          Unit: milliseconds
                          expr min lq mean median uq max neval
                          min_max 1.889293 1.9123860 2.08242647 1.9271395 2.0359730 3.527565 100
                          which1 9.809527 10.0342505 13.16711078 10.3671640 14.7839955 111.424664 100
                          which2 2.400745 2.4216995 2.66374110 2.4471435 2.5985265 4.259249 100





                          share|improve this answer













                          If you are a fan of efficiency, then you can use the function min_max from the Rfast package, with index = True



                          It will return the index of the minimum and the index of the maximum
                          value, simultaneously, faster than what has been disused so far.



                          E.g.



                          a = runif(10000)
                          Rfast::min_max(a,index=T)

                          # min max
                          # 2984 2885

                          which(a == min(a))

                          #[1] 2984

                          a = runif(1000000)
                          microbenchmark::microbenchmark(
                          min_max = Rfast::min_max(a,index=T),
                          which1 = which(a == min(a)),
                          which2 = which.min(a)
                          )

                          Unit: milliseconds
                          expr min lq mean median uq max neval
                          min_max 1.889293 1.9123860 2.08242647 1.9271395 2.0359730 3.527565 100
                          which1 9.809527 10.0342505 13.16711078 10.3671640 14.7839955 111.424664 100
                          which2 2.400745 2.4216995 2.66374110 2.4471435 2.5985265 4.259249 100






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 17 '18 at 14:37









                          StefanosStefanos

                          335313




                          335313

















                              protected by Sotos Aug 19 '17 at 14:47



                              Thank you for your interest in this question.
                              Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                              Would you like to answer one of these unanswered questions instead?



                              Popular posts from this blog

                              Guess what letter conforming each word

                              Port of Spain

                              Run scheduled task as local user group (not BUILTIN)