Return index of the smallest value in a vector?

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.)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)