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

The Archive Base Latest Questions

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

I have nested for loops as below. I need p:q=1:300, and n=20. Function mark

  • 0

I have nested for loops as below. I need p:q=1:300, and n=20. Function “mark” is the model of my interest(Package RMark). I know rbind can be slow but I have no idea what should be used to replace it. Otherwise what else I can do to make this function faster? Thanks.

foo<-function(data, p, q, n){
results.frame <- data.frame()
for (i in 1:n){
    for (i in p:q) {
        run.model<-mark(data[sample(nrow(data), i),], model="Occupancy")       
        results<-data.frame(summary(run.model)$real$p, Occupancy=summary(run.model)$real$Psi, se.p=t(as.matrix(summary(run.model, se=T)$real$p$se)), se.Psi=summary(run.model, se=T)$real$Psi$se, stations=i)
        results.frame<-rbind(results.frame, results)
        } 
    }
write.table(results.frame, "C:\\RWorkspace\\simulation_results.txt")
return(results.frame)
}
  • 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-10T14:18:51+00:00Added an answer on June 10, 2026 at 2:18 pm

    Yes, rbind can be slow; the faster thing is usually to make the matrix the right size to start with and fill it in appropriately. It’s also usually faster to fill in a matrix instead of a data frame.

    However, with the size you indicate, I would suspect that mark is what is slowing the function down and you won’t get much noticeable speedup by doing that. It would be easy to test that by storing a single result in run.model and then commenting that line out of your loop; that will tell you how much time it’s spending just storing the results. (You could also “profile” the function, but this would be simpler.)

    EDIT: I’m actually wrong; the size you indicate is big enough that the rbind is quite possible causing problems. On my system, which is fairly fast and has a decent amount of memory, it takes 7.73 sec to rbind using data frames with n=20 and only 0.09 sec with n=1, so clearly some memory churning is happening. As for speedup, with n=20 it takes only 1.00 sec to rbind matrices and 0.033 sec to fill it in.

    foo <- function(data, p, q, n){
      # make a single results line; remove this line when you put in your code 
      results <- c(1, Occupancy=2, se.p=3, se.Psi=4, stations=5)
      # make the matrix the right size to start with
      results.frame <- matrix(ncol=5, nrow=(q-p+1)*n)
      for (i in 1:n){
        for (j in p:q) {
          # get results here; commented out to show loop speed only
          # put in your actual code here instead
          results.frame[ 1+(i-1)*(q-p+1)+(j-p), ] <- results
        } 
      }
      # get the names right by taking the names from the last time through the loop
      colnames(results.frame) <- names(results)
      results.frame
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What if I have nested loops, and I want to break out of all
I have the following nested for loops. I want to parallelize the first loop
I have code in Java that has an outer loop and several nested loops.
I'm having a problem with some for nested loops that I have to convert
I have a nested JSON object that I need to loop through, and the
I would like to be able to have a series of nested loops that
I have a nested loop structure in my code. Sometimes, I get the below
I have a stored procedure below. In it is a nested loop. Instead of
i am working on a page wherein i have 2 nested loops, my problem
I have two nested loop in XSL like this, at this moment I use

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.