find closest number above in a list given an input

I would like to find, given an input number, the corresponding element of the list that is greater than or equal to the input number. I found an example solution online, but it only gives me the closest number, not the closest number that's greater than or equal to the input number (ie. it will select a number below the input if that number is closer)


[1] 90000

Here the code returns 90000, but I would need it to return 90001 since this is the closest number greater than or equal to the input


x <- c(1:10^6)
your_number <- 90000.43
which.min(abs(x - your_number))
#> [1] 90000

min(x[which(x > your_number)])
#> [1] 90001

Thank you so much technocrat!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.