Find index of value in a sorted vector in R

前端 未结 1 1306
南笙
南笙 2021-01-19 10:42

I have an ordered vector of unique integers in R and I want to find the index of the element closest to but less than or equal to some value. For example, for the vector

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-19 11:30

    Base R provides findInterval which implements a binary search:

    findInterval(17, c(4, 8, 15, 16, 23, 42))
    

    @Khashaa already mentioned this in a comment.

    0 讨论(0)
提交回复
热议问题