Find the index of minimum non negative value in R

data-manipulation, r

Solution

You can use

which.max(1 / x)
# [1] 3

Problem

Assume a numeric vector `x <- c(-3,2,1,-2, 4,-1,-5)` Min non-negative value in `x` is `1` so the index/location/answer should be `3`. How can we do it by using any function? (Note: Function `which.min`, in the above case, gives answer/index `7` that is the minimum (but not non negative))

Original source