0

I have a set of data (x1, x2, x3.....). From that, I need to select that particular data which is just below given data, say (X) in R The following sample and code give the value of X as 44.75. I need to write a select code that will select 43 from the dataset.

sample <- c(20,25,30,24,27,33,18,26,43, 57)
a <- (quantile(sample, 0.75))
b <- (quantile(sample, 0.25))
X <- a + 1.5*(a-b)
select <-
zanak02
  • 45
  • 4

1 Answers1

2

Using == with numeric works fine when the numbers do not have high precision (R FAQ 7.31 and Why are these numbers not equal?), so the other answer may suffice, but here are some options that are more robust in that way:

sort(sample[sample < X], decreasing = TRUE)[1]
# [1] 43

tmp <- sample[sample < X]
tmp[which.min(abs(tmp - X))]
# [1] 43
r2evans
  • 141,215
  • 6
  • 77
  • 149