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
Another one:
which.min(abs(18 - replace(f, f>18, Inf))) #[1] 6 f[which.min(abs(18 - replace(f, f>18, Inf)))] #[1] 12
Or as a function:
minsmaller <- function(x,value) which.min(abs(value - replace(x, x>value, Inf))) minsmaller(f, 18) #[1] 6 minsmaller(f, 19) #[1] 7