Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Following up @CarlWitthoft's answer, you probably want:</p> <pre><code>N &lt;- 5 rlex &lt;-NULL for (i in 1:N) { x &lt;-sample(0:1, 100000, 1/2) rlex &lt;-append(rlex, rle(x)$lengths) } </code></pre> <p>since I think you don't care about the <code>$values</code> component (i.e. whether each run is a run of zeros or ones).</p> <p><strong>Result</strong>: one long vector of run lengths.</p> <p>But this would probably be a lot more efficient:</p> <pre><code>maxlen &lt;- 30 rlemat &lt;- matrix(nrow=N,ncol=maxlen) for (i in 1:N) { x &lt;-sample(0:1, 100000, 1/2) rlemat[i,] &lt;- table(factor(rle(x)$lengths,levels=1:maxlen)) } </code></pre> <p><strong>Result</strong>: an <code>N</code> by <code>maxlen</code> table of run lengths from each iteration.</p> <p>If you only want to save the total number of runs of each length you could try:</p> <pre><code>rlecumsum &lt;- rep(0,maxlen) for (i in 1:N) { x &lt;-sample(0:1, 100000, 1/2) rlecumsum &lt;- rlecumsum + table(factor(rle(x)$lengths,levels=1:maxlen)) } </code></pre> <p><strong>Result</strong>: an vector of length <code>maxlen</code> of the total numbers of run lengths across all iterations.</p> <p>And here's my final answer:</p> <pre><code>rlecumtab &lt;- matrix(0,ncol=2,nrow=maxlen) for (i in 1:N) { x &lt;- sample(0:1, 100000, 1/2) r1 &lt;- rle(x) rtab &lt;- table(factor(r1$lengths,levels=1:maxlen),r1$values) rlecumtab &lt;- rlecumtab + rtab } </code></pre> <p><strong>Result</strong>: a <code>maxlen</code> by 2 table of the total numbers of run lengths across all iterations, divided by type (0-run vs 1-run).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload