r - return an index from a vector. the index holds a value which is most near to an given element -
i have list of elements such
a= 0.992688 0.892195 0.889151 0.380672 0.180576 0.685028 0.58195 given input element, 0.4, how can find index holds number beingness near number. instance, a[4] = 0.380672 near 0.4. therefore, should homecoming 4
one way:
# mnel points out in answer, difference, # using `which` here gives indices match which(abs(x-0.4) == min(abs(x-0.4))) where x vector.
alternately,
# 1 returns first index, slow sort(abs(x-0.4), index.return=t)$ix[1] r
No comments:
Post a Comment