Parth mehta
Parth mehta

Reputation: 1436

Get all the maximum value indexes in a R vector

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.

Upvotes: 4

Views: 7935

Answers (2)

vmorusu
vmorusu

Reputation: 936

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

Upvotes: 1

konvas
konvas

Reputation: 14346

How about which(v == max(v)) ?

Upvotes: 11

Related Questions