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
You could try:
x <- 18 f <- c(1,3,6,8,10,12,19,27) ifelse(x %in% f, which(f %in% x), which.min(abs(f - x)) - 1)
That way if x is not in f, it will return the nearest previous index. If x is in f, it will return x index.
x
f