I’ve got a set of numbers from 0 to 1. Given a value X in the set, I’d like to find the range values (high and low) where Y% of the values in the set are within high and low and where X is the mid point.
So let’s say the numbers are evenly distributed. Given X=0.4 and Y=20%, I need a function that will give me:
high = 0.5
low = 0.3
How can I do that in R?
Update: In light of the extra info from the comments, this will do what the OP wants:
Which gives the following on a sample of random values on interval 0,1:
The OP has answered the Qs below and the version of the function above does as was requested and in light of comments.
There are a couple of issues to deal with:
y% of the data is not an integer? At the moment, ify% of the data evaluates to say4.2I am rounding down tofloor(4.2)but we could round up toceiling(4.2).y/2% in any one direction? At the moment I return the extreme points of the data that lie above/below the mid point. This is a little inconsistent with the previous point though, should we return the extremes 0, 1 in this case too?Original: This will give you what you want, assuming the assumptions you state (evenly distributed on range 0,1)
We could extend the function to allow an arbitrary range with defaults 0, 1: