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 8868935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:22:11+00:00 2026-06-14T17:22:11+00:00

The great findInterval() function in R uses left-closed sub-intervals in its vec argument, as

  • 0

The great findInterval() function in R uses left-closed sub-intervals in its vec argument, as shown in its docs:

if i <- findInterval(x,v), we have v[i[j]] <= x[j] < v[i[j] + 1]

If I want right-closed sub-intervals, what are my options? The best I’ve come up with is this:

findInterval.rightClosed <- function(x, vec, ...) {
  fi <- findInterval(x, vec, ...)
  fi - (x==vec[fi])
}

Another one also works:

findInterval.rightClosed2 <- function(x, vec, ...) {
  length(vec) - findInterval(-x, -rev(vec), ...)
}

Here’s a little test:

x <- c(3, 6, 7, 7, 29, 37, 52)
vec <- c(2, 5, 6, 35)
findInterval(x, vec)
# [1] 1 3 3 3 3 4 4
findInterval.rightClosed(x, vec)
# [1] 1 2 3 3 3 4 4
findInterval.rightClosed2(x, vec)
# [1] 1 2 3 3 3 4 4

But I’d like to see any other solutions if there’s a better one. By “better”, I mean “somehow more satisfying” or “doesn’t feel like a kludge” or maybe even “more efficient”. =)

(Note that there’s a rightmost.closed argument to findInterval(), but it’s different – it only refers to the final sub-interval and has a different meaning.)

  • 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-06-14T17:22:12+00:00Added an answer on June 14, 2026 at 5:22 pm

    EDIT: Major clean-up in all aisles.

    You might look at cut. By default, cut makes left open and right closed intervals, and that can be changed using the appropriate argument (right). To use your example:

    x <- c(3, 6, 7, 7, 29, 37, 52)
    vec <- c(2, 5, 6, 35)
    cutVec <- c(vec, max(x)) # for cut, range of vec should cover all of x
    

    Now create four functions that should do the same thing: Two from the OP, one from Josh O’Brien, and then cut. Two arguments to cut have been changed from default settings: include.lowest = TRUE will create an interval closed on both sides for the smallest (leftmost) interval. labels = FALSE will cause cut to return simply the integer values for the bins instead of creating a factor, which it otherwise does.

    findInterval.rightClosed <- function(x, vec, ...) {
      fi <- findInterval(x, vec, ...)
      fi - (x==vec[fi])
    }
    findInterval.rightClosed2 <- function(x, vec, ...) {
      length(vec) - findInterval(-x, -rev(vec), ...)
    }
    cutFun <- function(x, vec){
        cut(x, vec, include.lowest = TRUE, labels = FALSE)
    }
    # The body of fiFun is a contribution by Josh O'Brien that got fed to the ether.
    fiFun <- function(x, vec){
        xxFI <- findInterval(x, vec * (1 + .Machine$double.eps))
    }
    

    Do all functions return the same result? Yup. (notice the use of cutVec for cutFun)

    mapply(identical, list(findInterval.rightClosed(x, vec)),
      list(findInterval.rightClosed2(x, vec), cutFun(x, cutVec), fiFun(x, vec)))
    # [1] TRUE TRUE TRUE
    

    Now a more demanding vector to bin:

    x <- rpois(2e6, 10)
    vec <- c(-Inf, quantile(x, seq(.2, 1, .2)))
    

    Test whether identical (note use of unname)

    mapply(identical, list(unname(findInterval.rightClosed(x, vec))),
      list(findInterval.rightClosed2(x, vec), cutFun(x, vec), fiFun(x, vec)))
    # [1] TRUE TRUE TRUE
    

    And benchmark:

    library(microbenchmark)
    microbenchmark(findInterval.rightClosed(x, vec), findInterval.rightClosed2(x, vec),
      cutFun(x, vec), fiFun(x, vec), times = 50)
    # Unit: milliseconds
    #                                expr       min        lq    median        uq       max
    # 1                    cutFun(x, vec)  35.46261  35.63435  35.81233  36.68036  53.52078
    # 2                     fiFun(x, vec)  51.30158  51.69391  52.24277  53.69253  67.09433
    # 3  findInterval.rightClosed(x, vec) 124.57110 133.99315 142.06567 155.68592 176.43291
    # 4 findInterval.rightClosed2(x, vec)  79.81685  82.01025  86.20182  95.65368 108.51624
    

    From this run, cut seems to be the fastest.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a great problem about the rotation in three.js I want to rotate
Great site! I have a question I haven't seen answered. I am very new
Great toolkit and great demos! I want to use XTK with an existing system.
I have found many great answers to this from a year ago (when it
with great difficulty , I guess I have finally been able to find the
All great stories they always start with those 4 magical words... I have inherited
I've a great problem. I must pass a variable of a javascript function into
I am using the great RestKit Framework for an iPhone Application. I have got
great community :) I have category list which have nested child category list, which
Hi great people of stackoverflow, I have a problem with my input box where

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.