 <p>Unless you really need them, it's easiest not to store the numbers you generate any longer than you need, as there will be 250640000 of them, taking about a Gb of memory. Instead, you can just call <code>table</code> immediately on them. Using <code>replicate</code> instead of a <code>for</code> loop (you're not using <code>i</code> anyway) will put the results into a nice matrix for you too:</p> <pre><code>Record &lt;- replicate(5000, table(sample(x = 0:9, size = 50128, replace = T))) Record[, 1:10] ## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] ## 0 4999 4971 4919 5116 5101 5016 4861 5109 5063 4991 ## 1 5060 4959 4935 5047 5142 4927 5005 4920 5098 5023 ## 2 4916 4954 5019 4966 4994 4954 5049 5013 5031 5081 ## 3 4907 5007 5075 5042 4993 5015 5078 5037 4936 5073 ## 4 5117 4935 5003 5000 4970 5084 5008 4948 5115 5010 ## 5 4966 5146 5054 4944 5048 4935 5016 5104 5042 5010 ## 6 5055 4899 4964 5006 4977 5076 4943 5048 5064 4817 ## 7 5082 5040 5070 5034 4976 5056 5049 5012 4985 4983 ## 8 5094 5108 5014 4949 5052 5037 5073 5000 4894 5082 ## 9 4932 5109 5075 5024 4875 5028 5046 4937 4900 5058 </code></pre> <p>If you really need to store the numbers as well, you can use a similar approach:</p> <pre><code># Make a matrix of terms. Warning: big. Terms &lt;- replicate(5000, sample(x = 0:9, size = 50128, replace = T)) # Apply table to each column (set of samples) Record &lt;- apply(Terms, 2, table) </code></pre> <p>Alternately, if you're storing the samples, instead of using <code>replicate</code> you could just sample directly into a matrix:</p> <pre><code>Terms &lt;- matrix(sample(x = 0:9, size = 50128 * 5000, replace = T), nrow = 50128) Record &lt;- apply(Terms, 2, table) </code></pre> <p><code>Record</code> (and <code>Terms</code>, if stored) will look the same regardless, though the time required may vary.</p>