How to identify coordinates in R

前端 未结 3 1511
[愿得一人]
[愿得一人] 2021-01-17 08:25

I would like to identify which coordinate of my vector gives me the greatest value. For a simple example suppose that:

x <- c(10,22,20,18,5)
3条回答
  •  半阙折子戏
    2021-01-17 09:03

    First, find the greatest value with max:

    > max(x)
    [1] 22
    

    From there, you can figure out which value(s) in the vector match the greatest value:

    > x==max(x)
    [1] FALSE  TRUE FALSE FALSE FALSE
    

    which() can be used to translate the boolean vector into indices:

    which(x==max(x))
    [1] 2
    

提交回复
热议问题