Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If the goal is to create two big samples, consider the following:</p> <pre><code>N &lt;- 2000000 A &lt;- sample(1:5, N, 1/5) B &lt;- sample(1:5, N, 1/5) save.image() </code></pre> <p>If it is important that the samples be formed 1000 at a time for A and B alternatively, consider this:</p> <pre><code>N &lt;- 2000 n &lt;- 1000 A.list &lt;- vector("list", N) B.list &lt;- vector("list", N) for (i in 1:N) { A.list[[i]] &lt;- sample(1:5, n, 1/5) B.list[[i]] &lt;- sample(1:5, n, 1/5) } A &lt;- unlist(A.list) B &lt;- unlist(B.list) save.image() </code></pre> <p>This should take care of the two main issues in your code:</p> <ul> <li>every time you use <code>append</code> inside your loop, R has to create and fill a couple new objects from scratch. As the objects become larger, your loop iterations becomes slower and slower; computation times grow quadratically I believe. You also run the risk of fragmenting your memory space, this is harder to explain but you can try to research it. By using a list, only the new data from each iteration needs to be stored to memory and the computation time per loop remains the same.</li> <li>I have moved save.image() outside of the loop. Same idea, saving objects as they get bigger and bigger will take longer and longer, i.e. slow down your iterations. Since you only care for the final vector, it makes sense to only save when you are done.</li> </ul> <p>You can play with the value of <code>N</code> to see how far your OS will let you go. The advantage is that you don't have to wait for a week or a month to find out what the limits are.</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