Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, so your code only has one small bug, but there are definitely better ways of doing this. Your code doesn't work when the number of rows is an exact multiple of <code>step</code>. This has to do with the position of your <code>break</code>. Here is a fix:</p> <pre><code>create_frame_list&lt;-function(mydata,mystep,elnames){ datalim&lt;-dim(mydata)[1] mylist&lt;-list() init&lt;-1 top&lt;-mystep i&lt;-1 repeat{ if(top &lt; datalim) # mylist[[i]]&lt;-assign(paste0(elnames,as.character(i)),data.frame(mydata[init:top,])) mylist[[i]]&lt;-mydata[init:top,] else mylist[[i]]&lt;-mydata[init:datalim,] # if(top &gt; datalim) break i&lt;-i+1 init&lt;-top+1 top&lt;-top+mystep if(init &gt; datalim) break } return(mylist) } </code></pre> <p>The main fix was to move the <code>if</code> and make it reliant on <code>init</code>, and not <code>top</code>. </p> <p>You'll note that I cleaned up your code, and removed the <code>assign</code> statments. One good rule of thumb is: if you think you need to use <code>assign</code> or <code>get</code>, you're doing it wrong. In your case, the assign was completely redundant, and did not assign the names in the way you wanted.</p> <hr> <p>If you're looking for a better way to do this, here is one option:</p> <pre><code>n&lt;-nrow(test_data) step&lt;-300 split.var&lt;-rep(1:ceiling(n/step),each=step,length.out=n) master_list&lt;-split(test_data,split.var) names(master_list)&lt;-paste0('bd',seq_along(master_list)) # If you didn't care about the order of the rows you could just do # split(test_data,seq(ceiling(n/step))) </code></pre> <hr> <p>If you want to get fancy, you could do something like:</p> <pre><code>special.split&lt;-function(data,step) split(data,rep(1:ceiling(nrow(data)/step),each=step,length.out=nrow(data))) lapply(special.split(test_data,300),special.split,step=50) </code></pre> <p>And that would do everything in one step.</p>
    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.
 

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