title: R: Row resampling loop speed improvement
body: <p>I'm subsampling rows from a dataframe with c("x","y","density") columns at a variety of c("s_size","reps"). Reps= replicates, s_size= number of rows subsampled from the whole dataframe.</p>
  
  <pre><code>&gt; head(data_xyz)
     x y density
  1  6 1       0
  2  7 1   17600
  3  8 1   11200
  4 12 1   14400
  5 13 1       0
  6 14 1    8000
  
  
  
   #Subsampling###################
      subsample_loop &lt;- function(s_size, reps, int) {
        tm1 &lt;- system.time( #start timer
      {
        subsample_bound = data.frame()
      #Perform Subsampling of the general 
      for (s_size in seq(1,s_size,int)){
        for (reps in 1:reps) {
          subsample &lt;- sample.df.rows(s_size, data_xyz)
           assign(paste("sample" ,"_","n", s_size, "_", "r", reps , sep=""), subsample)
          subsample_replicate &lt;- subsample[,] #temporary variable
          subsample_replicate &lt;- cbind(subsample, rep(s_size,(length(subsample_replicate[,1]))),
                                       rep(reps,(length(subsample_replicate[,1]))))
          subsample_bound &lt;- rbind(subsample_bound, subsample_replicate)
  
        }
      }
      }) #end timer
        colnames(subsample_bound) &lt;- c("x","y","density","s_size","reps")
      subsample_bound
      } #end function
  
  Here's the function call:
  
      source("R/functions.R")
      subsample_data &lt;- subsample_loop(s_size=206, reps=5, int=10)
  </code></pre>
  
  <p>Here's the row subsample function:</p>
  
  <pre><code># Samples a number of rows in a dataframe, outputs a dataframe of the same # of columns
  # df Data Frame
  # N number of samples to be taken
  sample.df.rows &lt;- function (N, df, ...) 
    { 
      df[sample(nrow(df), N, replace=FALSE,...), ] 
    } 
  </code></pre>
  
  <p>It's way too slow, I've tried a few times with apply functions and had no luck. I'll be doing somewhere around 1,000-10,000 replicates for each s_size from 1:250.</p>
  
  <p>Let me know what you think! Thanks in advance.</p>
  
  <p>=========================================================================
  UPDATE EDIT: Sample data from which to sample:
  <a href="https://www.dropbox.com/s/47mpo36xh7lck0t/density.csv" rel="nofollow">https://www.dropbox.com/s/47mpo36xh7lck0t/density.csv</a></p>
  
  <p>Joran's code in a function (in a sourced function.R file):</p>
  
  <pre><code>foo &lt;- function(i,j,data){
    res &lt;- data[sample(nrow(data),i,replace = FALSE),]
    res$s_size &lt;- i
    res$reps &lt;- rep(j,i)
    res
  }
  resampling_custom &lt;- function(dat, s_size, int, reps) {
    ss &lt;- rep(seq(1,s_size,by = int),each = reps)
    id &lt;- rep(seq_len(reps),times = s_size/int)
    out &lt;- do.call(rbind,mapply(foo,i = ss,j = id,MoreArgs = list(data = dat),SIMPLIFY = FALSE))
  }
  </code></pre>
  
  <p>Calling the function</p>
  
  <pre><code>set.seed(2)
  out &lt;- resampling_custom(dat=retinal_xyz, s_size=206, int=5, reps=10)
  </code></pre>
  
  <p>outputs data, unfortunately with this <strong>warning message:</strong></p>
  
  <pre><code>Warning message:
  In mapply(foo, i = ss, j = id, MoreArgs = list(data = dat), SIMPLIFY = FALSE) :
    longer argument not a multiple of length of shorter
  </code></pre>