Home > Software engineering >  "Sandwiching" Values Between Rows
"Sandwiching" Values Between Rows

Time:06-12

I have this dataset:

set.seed(111)
row_num = 1:100
variable = rexp(100,1/100)
sandwich = data.frame(row_num, variable)
sandwich = sandwich[order(sandwich$variable),]

Suppose I have some new data point:

new_point = 4.1

I want to identify the two rows in the data that closest to this new_point (4.1) - one slightly greater than 4.1 and one smaller slightly smaller than 4.1.

I thought of this:

sandwich$diff = abs(sandwich$variable - new_point)
sandwich = sandwich[order(sandwich$diff),]
final_rows = sandwich[c(1,2),]

I am not sure if this is the correct and most efficient way to solve this problem - can someone please comment on this?

Thank you!

CodePudding user response:

An option is also with findInterval

i1 <- findInterval(new_point, sandwich$variable)

-checking

> sandwich[i1:(i1 1),]
   row_num variable
40      40 3.643575
8        8 6.459048
  • Related