How to know in which position an unknown is located in a vector?

Asked

Viewed 53 times

1

Example:

a=c(10,9,8,7)
b<-max(a)
a[b]

I need to know in which position b is located in vector a, R must return 1 (which is the answer)

2 answers

4


Take a look at the function which():

a=c(10,9,8,7)
b<-max(a)
a[b]
which(a == b)
# [1] 1

Other options is the function match():

match(b, a)
# [1] 1
  • Thank you very much, I am in the middle of a gigantic code for a job and did not know of this basic command! Grateful!

4

Another way to solve is with the function which.max:

a=c(10,9,8,7)
which.max(a)
[1] 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.