Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>Update</h3> <p>Not knowing what you are trying to do, I'll share one more suggestion: Preallocate vectors of the type you want for each column, insert values into those vectors, and then, at the end, create your <code>data.frame</code>.</p> <p>Continuing with Julian's <code>f3</code> (a preallocated <code>data.frame</code>) as the fastest option so far, defined as:</p> <pre><code># pre-allocate space f3 &lt;- function(n){ df &lt;- data.frame(x = numeric(n), y = character(n), stringsAsFactors = FALSE) for(i in 1:n){ df$x[i] &lt;- i df$y[i] &lt;- toString(i) } df } </code></pre> <p>Here's a similar approach, but one where the <code>data.frame</code> is created as the last step.</p> <pre><code># Use preallocated vectors f4 &lt;- function(n) { x &lt;- numeric(n) y &lt;- character(n) for (i in 1:n) { x[i] &lt;- i y[i] &lt;- i } data.frame(x, y, stringsAsFactors=FALSE) } </code></pre> <p><code>microbenchmark</code> from the "microbenchmark" package will give us more comprehensive insight than <code>system.time</code>:</p> <pre><code>library(microbenchmark) microbenchmark(f1(1000), f3(1000), f4(1000), times = 5) # Unit: milliseconds # expr min lq median uq max neval # f1(1000) 1024.539618 1029.693877 1045.972666 1055.25931 1112.769176 5 # f3(1000) 149.417636 150.529011 150.827393 151.02230 160.637845 5 # f4(1000) 7.872647 7.892395 7.901151 7.95077 8.049581 5 </code></pre> <p><code>f1()</code> (the approach below) is incredibly inefficient because of how often it calls <code>data.frame</code> and because growing objects that way is generally slow in R. <code>f3()</code> is much improved due to preallocation, but the <code>data.frame</code> structure itself might be part of the bottleneck here. <code>f4()</code> tries to bypass that bottleneck without compromising the approach you want to take.</p> <hr> <h3>Original answer</h3> <p>This is really not a good idea, but if you wanted to do it this way, I guess you can try:</p> <pre><code>for (i in 1:10) { df &lt;- rbind(df, data.frame(x = i, y = toString(i))) } </code></pre> <p>Note that in your code, there is one other problem:</p> <ul> <li>You should use <code>stringsAsFactors</code> if you want the characters to not get converted to factors. Use: <code>df = data.frame(x = numeric(), y = character(), stringsAsFactors = FALSE)</code></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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