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

The Archive Base Latest Questions

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

I am running a coin-toss simulation with a loop which runs about 1 million

  • 0

I am running a coin-toss simulation with a loop which runs about 1 million times.

Each time I run the loop I wish to retain the table output from the RLE command. Unfortunately a simple append does not seem to be appropriate. Each time I run the loop I get a slightly different amount of data which seems to be one of the sticking points.

This code gives an idea of what I am doing:

N <- 5 #Number of times to run
rlex <-NULL
#begin loop#############################
for (i in 1:N) { #tells R to repeat N number
x <-sample(0:1, 100000, 1/2)
rlex <-append(rlex, rle(x))
}
table(rlex) #doesn't work
table(rle(x)) #only 1

So instead of having five separate rle results (in this simulation, 1 million in the full version), I want one merged rle table. Hope this is clear. Obviously my actual code is a bit more complex, hence any solution should be as close to what I have specified as possible.

UPDATE: The loop is an absolute requirement. No ifs or buts. Perhaps I can pull out the table(rle(x)) data and put it into a matrix. However again the stumbling block is the fact that some of the less frequent run lengths do not always turn up in each loop. Thus I guess I am looking to conditionally fill a matrix based on the run length number?

Last update before I give up: Retaining the rle$values will mean that too much data is being retained. My simulation is large-scale and I really only wish to retain the table output of the rle. Either I retain each table(rle(x)) for each loop and combine by hand (there will be thousands), or I find a programmatic way to keep the data (yes for zeroes and ones) and have one table that is formed from merging each of the individual loops as I go along.

Either this is easyish to do, as specified, or I will not be doing it. It may seem a silly idea/request, but that should be incidental to whether it can be done.

Seriously last time. Here is an animated gif showing what I expect to happen. enter image description here

After each iteration of the loop data is added to the table. This is as clear as I am going to be able to communicate it.

  • 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-13T02:59:11+00:00Added an answer on June 13, 2026 at 2:59 am

    OK, attempt number 4:

    N <- 5
    set.seed(1)
    x <- NULL
    for (i in 1:N){
      x <- rbind(x, table(rle(sample(0:1, 100000, replace=TRUE))))
    }
    
    x <- as.data.frame(x)
    x$length <- as.numeric(rownames(x))
    aggregate(x[, 1:2], list(x[[3]]), sum)
    

    Produces:

       Group.1     0     1
    1        1 62634 62531
    2        2 31410 31577
    3        3 15748 15488
    4        4  7604  7876
    5        5  3912  3845
    6        6  1968  1951
    7        7   979   971
    8        8   498   477
    9        9   227   246
    10      10   109   128
    11      11    65    59
    12      12    24    30
    13      13    21    11
    14      14     7    10
    15      15     0     4
    16      16     4     2
    17      17     0     1
    18      18     0     1
    

    If you want the aggregation inside the loop, do:

    N <- 5
    set.seed(1)
    x <- NULL
    for (i in 1:N){
      x <- rbind(x, table(rle(sample(0:1, 100000, replace=TRUE))))
      y <- aggregate(x, list(as.numeric(rownames(x))), sum)
      print(y)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Running an almost trivial script in lua with dofile, 10000 times, takes about 52
Running this program is printing forked! 7 times. Can someone explain how forked! is
Running this query takes a long time: SELECT host,ip FROM arecords WHERE ip IN
I am writing a simple C program that tosses a coin 100000 times and
im trying to fix this while loop but keep running into errors. Basically lets
Running a rails site right now using SQLite3. About once every 500 requests or
Running OS X Leopard an MacBook Pro from Jan. 2008. I used to run
Let's say I have this piece of code running inside a loop where $fid
Running Solr on Tomcat 7 on Win 2008 Server. I am looping through a
Running my script through Devel::NYTProf showed that the following portion of code took up

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.