Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can <code>scan</code> and then <code>write</code> to a file(s) one line at a time.</p> <pre><code>i &lt;- 0 while({x &lt;- scan("file.csv", sep = ",", skip = i, nlines = 1, what = "character"); length(x) &gt; 1}) { write(x[1], "file1.csv", sep = ",", append = T) write(x[2], "file2.csv", sep = ",", append = T) write(x[3], "file3.csv", sep = ",", append = T) i &lt;- i + 1 } </code></pre> <p><strong>edit!! I am using the above data, copied over 1000 times. I've done a comparison of speed when we have the file connection open at all times.</strong></p> <pre><code>ver1 &lt;- function() { i &lt;- 0 while({x &lt;- scan("file.csv", sep = ",", skip = i, nlines = 1, what = "character"); length(x) &gt; 1}) { write(x[1], "file1.csv", sep = ",", append = T) write(x[2], "file2.csv", sep = ",", append = T) write(x[3], "file3.csv", sep = ",", append = T) i &lt;- i + 1 } } system.time(ver1()) # w/ close to 3K lines of data, 3 columns ## user system elapsed ## 2.809 0.417 3.629 ver2 &lt;- function() { f &lt;- file("file.csv", "r") f1 &lt;- file("file1.csv", "w") f2 &lt;- file("file2.csv", "w") f3 &lt;- file("file3.csv", "w") while({x &lt;- scan(f, sep = ",", skip = 0, nlines = 1, what = "character"); length(x) &gt; 1}) { write(x[1], file = f1, sep = ",", append = T, ncol = 1) write(x[2], file = f2, sep = ",", append = T, ncol = 1) write(x[3], file = f3, sep = ",", append = T, ncol = 1) } closeAllConnections() } system.time(ver2()) ## user system elapsed ## 0.257 0.098 0.409 </code></pre>
 

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