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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:56:13+00:00 2026-06-13T03:56:13+00:00

I have a working solution to my problem, but I will not be able

  • 0

I have a working solution to my problem, but I will not be able to use it because it is so slow (my calculations predict that the whole simulation will take 2-3 years!). Thus I am looking for a better (faster) solution. This is (in essence) the code I am working with:

N=4
x <-NULL
for (i in 1:N) { #first loop
  v <-sample(0:1, 1000000, 1/2) #generate data
  v <-as.data.frame(v) #convert to dataframe
  v$t <-rep(1:2, each=250) #group
  v$p <-rep(1:2000, each=500) #p.number
  # second loop
  for (j in 1:2000) { #second loop
    #count rle for group 1 for each pnumber
    x <- rbind(x, table(rle(v$v[v$t==1&v$p==j])))
    #count rle for group 2 for each pnumber
    x <- rbind(x, table(rle(v$v[v$t==2&v$p==j])))
  } #end second loop
} #end first loop
#total rle counts for both group 1 & 2
y <-aggregate(x, list(as.numeric(rownames(x))), sum)

In words: The code generates a coin-flip simulation (v). A group factor is generated (1 & 2). A p.number factor is generated (1:2000). The run lengths are recorded for each p.number (1:2000) for both groups 1 & group 2 (each p.number has runs in both groups). After N loops (the first loop), the total run lengths are presented as a table (aggregate) (that is, the run lengths for each group, for each p.number, over N loops as a total).

I need the first loop because the data that I am working with comes in individual files (so I’m loading the file, calculating various statistics etc and then loading the next file and doing the same). I am much less attached to the second loop, but can’t figure out how to replace it with something faster.

What can be done to the second loop to make it (hopefully, a lot) faster?

  • 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-13T03:56:14+00:00Added an answer on June 13, 2026 at 3:56 am

    If you just want to run rle and table for each combination of the values of v$t and v$p separately, there is no need for the second loop. It is much faster in this way:

    values <- v$v + v$t * 10 + v$p * 100
    runlength <- rle(values)
    runlength$values <- runlength$values %% 2
    x <- table(runlength)
    
    
    y <- aggregate(unclass(x), list(as.numeric(rownames(x))), sum)
    

    The whole code will look like this. If N is as low as 4, the growing object x will not be a severe problem. But generally I agree with @GavinSimpson, that it is not a good programming technique.

    N=4
    x <-NULL
    for (i in 1:N) { #first loop
      v <-sample(0:1, 1000000, 1/2) #generate data
      v <-as.data.frame(v) #convert to dataframe
      v$t <-rep(1:2, each=250) #group
      v$p <-rep(1:2000, each=500) #p.number
    
      values <- v$v + N * 10 + v$t * 100 + v$p * 1000
      runlength <- rle(values)
      runlength$values <- runlength$values %% 2
      x <- rbind(x, table(runlength))
    
    } #end first loop
    y <-aggregate(x, list(as.numeric(rownames(x))), sum) #tota
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have working registration script the only problem is that i do not know
I have a small problem but couldn't find solution for that. I have founded
Currently working with Oracle, but will also need a solution for MS SQL. I
I have tried a lot and have not yet able to find solution to
We have a SOAP connection to magento that is working great - we're able
Hi I have been working on a solution for days, and I've been trying
Iam working on small booking room system. In my solution I have a Reservation
I have a VS2010 solution which uses its own projects (that are in that
At work, I have started working on a program that can potentially generate hundreds
I have an iPhone app that is using sqlite 3.6 (not with FMDB) to

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.