title: Loop to split Large Data Frame and write multiple CSV files in R
body: <p>I have a file with 25 million rows and need to split it into smaller files based on the factor levels. I created a dataframe to include distinct factor levels and wrote a loop to perform some operations and write out a <code>csv</code>.</p>
  <p>data looks like this:</p>
  <pre><code>Country Col2 Code   Year
   A       C     1    2020
   A       D     1    2020
   A       C     1    2020
   A       D     2    2020
   A       C     2    2020
   A       D     2    2020
   A       C     2    2020
   A       D     3    2020
  </code></pre>
  <p>Intention is to write a csv file for every subset based on code</p>
  <pre><code>d1 &lt;- data %&gt;%
    distinct(Code)
  </code></pre>
  <pre><code>for(i in 1:nrow(d1))
  {
    
    subset &lt;- data %&gt;%
    filter(Code  == Code[i])
    co &lt;- subset$Code[i]
    
    yr&lt;- subset$Year[i]
    
  
    setwd(&quot;C:/Users/...&quot;)
    
    write.csv(subset, paste(co,&quot;_&quot;,Year, &quot;.csv&quot;, sep=&quot;&quot;), append = FALSE, row.names = FALSE)
    
    }
  
  </code></pre>
  <p>The output keeps getting written to the same file instead of creating separate files in the directory.</p>
  <p>IS there any better way of doing this? Thank you.</p>