Reputation: 649
I'm trying to make a function that determines what bucket a certain value goes into based off of a given vector. So my function has two inputs: a vector determining the break points for the bucket (ex: if the vector is (1,4,5,10) the buckets would be <=1, 110) and a certain number. I want the function to output a certain value determining the bucket. For example if I input .9 the output could be 1, 1.6, the output could be 4, 5.8 the output could be 10, and 13, the output could be "10+".
The way I'm doing it right now is I first check if the input number is bigger than the vector's largest element or smaller than the vector's smallest element. If not, I then run a for loop (can't figure out how to use apply) to check if the number is in each specific interval. The problem is this is way too inefficient because I'm dealing with a large data set. Does anyone know an efficient way to do this?
Upvotes: 2
Views: 2398
Reputation: 3388
The cut()
function is convenient for bucketing: cut(splitme,breaks=vectorwithsplits)
.
However, it looks like you're actually trying to figure out an insertion point. You need something like binary search.
Upvotes: 4