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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:49:10+00:00 2026-06-13T15:49:10+00:00

I have two vector e and g . I want to know for each

  • 0

I have two vector e and g. I want to know for each element in e the percentage of elements in g that are smaller. One way to implement this in R is:

set.seed(21)
e <- rnorm(1e4)
g <- rnorm(1e4)
mf <- function(p,v) {100*length(which(v<=p))/length(v)}
mf.out <- sapply(X=e, FUN=mf, v=g)

With large e or g, this takes a lot of time to run. How can I change or adapt this code to make this run faster?

Note: The mf function above is based on code from the mess function in the dismo package.

  • 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-13T15:49:12+00:00Added an answer on June 13, 2026 at 3:49 pm

    The reason this is so slow is because you’re calling your function length(e) times. It doesn’t make a large difference for small vectors, but the overhead from R function calls really starts to add up with larger vectors.

    Normally, you would need to move this to compiled code, but luckily you can use findInterval:

    set.seed(21)
    e <- rnorm(1e4)
    g <- rnorm(1e4)
    O <- findInterval(e,sort(g))/length(g)
    
    # Now for some timings:
    f <- function(p,v) mean(v<=p)
    system.time(o <- sapply(e, f, g))
    #   user  system elapsed 
    #   0.95    0.03    0.98
    system.time(O <- findInterval(e,sort(g))/length(g))
    #   user  system elapsed 
    #      0       0       0 
    identical(o,O)  # may be FALSE
    all.equal(o,O)  # should be TRUE
    
    # How fast is this on large vectors?
    set.seed(21)
    e <- rnorm(1e7)
    g <- rnorm(1e7)
    system.time(O <- findInterval(e,sort(g))/length(g))
    #   user  system elapsed 
    #  22.08    0.08   22.31
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two std::list , which each one has a positional vector m_Pos .
I am trying to multiply two vectors together where each element of one vector
I have a relatively simple algorithm that walks an std::vector looking for two neighbouring
I have two vectors, x and y . x is a vector where each
I have this two vector<double> 's mass and velocity both of the same size
I have two ordered numpy arrays and I want to interleave them so that
I have a WPF window that is supposed to load two vector images from
I have the two big and small list. I want to know which of
I have two vc++ dll projects, which will generate two dlls. A vector<vector<string>> object
I have two vectors, v1 and v2 v1 <- c('one', 'two', 'three') v2 <-

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.