Find nearest smaller number

前端 未结 7 1175
粉色の甜心
粉色の甜心 2021-01-18 12:35

I have a vector of numbers

f <- c(1, 3, 5, 8, 10, 12, 19, 27)

I want to compare the values in the vector to another number, and find the c

7条回答
  •  离开以前
    2021-01-18 12:39

    I think this answer is pretty straightforward:

    f <- c(1,3,6,8,10,12,19,27)
    x <- 18
    
    # find the value that is closest to x
    maxless <- max(f[f <= x])
    # find out which value that is
    which(f == maxless)
    

提交回复
热议问题