Get all the maximum value indexes in a R vector

谁说我不能喝 提交于 2020-07-03 08:21:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!