问题
Lets say we have a vector in R :
v <- (2, 3, 4, 5, 5, 5)
We can easily find the max of the vector using max function :
max(v)
How can we find all the indexes where the max value is present. There is function which.max(v) which only returns the first index. Is there an easy way to get all the indexes having max values in R ?
Its a dummy question, but just curious to know.
回答1:
How about which(v == max(v))
?
回答2:
As @konvas sol gives indices, adding code snippet on how to retrieve elements. Just to help a newbie like me to understand its usage.
This fetches the largest words from the vector 'words'
words[which (nchar(words) == max(nchar(words)))]
来源:https://stackoverflow.com/questions/26693693/get-all-the-maximum-value-indexes-in-a-r-vector