Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4327246
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:30:40+00:00 2026-05-21T09:30:40+00:00

I’ve got a set of numbers from 0 to 1. Given a value X

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:30:40+00:00Added an answer on May 21, 2026 at 9:30 am

    Update: In light of the extra info from the comments, this will do what the OP wants:

    foobar <- function(x, mid, y) {
        ## x, input data on range 0,1
        ## mid, midpoint X in OP's Q
        ## y, % of points around mid
        sx <- sort(x)
        want <- sx >= mid
        ## what do you want to do if y% of x is not integer?
        num <- floor(((y/100) * length(x)) / 2)
        high <- if((len <- length(want[want])) == 0) {
            1
        } else {
            if(len < num) {
                tail(sx, 1)
            } else {
                sx[want][num]
            }
        }
        low <- if((len <- length(want[!want])) == 0) {
            0
        } else {
            if(len < num) {
                head(sx, 1)
            } else {
                rev(sx[!want])[num]
            }
        }
        res <- c(low, high)
        names(res) <- c("low","high")
        res
    }
    

    Which gives the following on a sample of random values on interval 0,1:

    > set.seed(1)
    > x <- runif(20)
    > sort(x)
     [1] 0.06178627 0.17655675 0.20168193 0.20597457 0.26550866 0.37212390
     [7] 0.38003518 0.38410372 0.49769924 0.57285336 0.62911404 0.66079779
    [13] 0.68702285 0.71761851 0.76984142 0.77744522 0.89838968 0.90820779
    [19] 0.94467527 0.99190609
    > foobar(x, 0.4, 20)
          low      high 
    0.3800352 0.5728534
    

    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:

    • What do you want to do if y% of the data is not an integer? At the moment, if y% of the data evaluates to say 4.2 I am rounding down to floor(4.2) but we could round up to ceiling(4.2).
    • What do you want to do if there are 0 values above or below the chosen mid point? At the moment the code returns the stated extremes (0,1) in those cases.
    • What do you want to do if there are some values above/below the mid point but not enough in a given direction to encompass 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)

    foo <- function(x, y) {
        ## x is the mid-point
        ## y is the % range about x, i.e. y/2 either side of x
        x + (c(-1,1) * (((y/100) / 2) * 1))
    }
    
    > foo(0.4, 20)
    [1] 0.3 0.5
    

    We could extend the function to allow an arbitrary range with defaults 0, 1:

    bar <- function(x, y, min = 0, max = 1) {
        ## x is the mid-point
        ## y is the % range about x, i.e. y/2 either side of x
        ## min, max, the lower and upper bounds on the data
        stopifnot(x >= min & x <= max)
        x + (c(-1,1) * (((y/100) / 2) * (max - min)))
    }
    
    > bar(0.4, 20)
    [1] 0.3 0.5
    > bar(0.6, 20, 0.5, 1)
    [1] 0.55 0.65
    > bar(0.4, 20, 0.5, 1)
    Error: x >= min & x <= max is not TRUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.