title: R - convert <.out> into <.csv> format
body: <p>I need to convert model output files into <code>.csv</code> format. The original files are in <code>.out</code> format, and are tables of data delimited by several spaces in between:</p>
  
  <p>e.g. The original files have about 14,000 rows, but the structure is the same.
  Please, use this link for an example file:
  <a href="https://www.dropbox.com/sh/1w0f9cq2w6gcbjo/AACoGu6L3yKBGBKODY87lygsa?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/sh/1w0f9cq2w6gcbjo/AACoGu6L3yKBGBKODY87lygsa?dl=0</a> </p>
  
  <pre><code>time   dayofyr   nit_N2O-N  dnit_N2O-N   dnit_N2-N        NO-N  CUM-N2O(gN/ha)  CUM-NO(gN/ha)
  1980.00     1      0.3310        0.3403        0.2022        0.0000        0.6713        0.0000  
  1980.00     2      0.3295        0.3400        0.2020        0.0000        1.3408        0.0000  
  1980.00     3      0.3280        0.3397        0.2018        0.0000        2.0086        0.0000  
  1980.00     4      0.3265        0.3395        0.2016        0.0000        2.6746        0.0000  
  1980.00     5      0.3251        0.3393        0.2015        0.0000        3.3389        0.0000  
  1980.00     6      0.3237        0.3391        0.2014        0.0000        4.0017        0.0000  
  1980.00     7      0.3223        0.3389        0.2013        0.0000        4.6629        0.0000  
  1980.00     8      0.3209        0.3387        0.2011        0.0000        5.3225        0.0000  
  1980.00     9      0.3195        0.3386        0.2010        0.0000        5.9805        0.0000  
  1980.00    10      0.3198        0.4589        0.2868        0.0000        6.7592        0.0000  
  </code></pre>
  
  <p>I have tried changing extension to .csv and converting consecutive spaces into ",", but I am afraid there's something I am missing, either in the regex syntax or in the overall rationale.</p>
  
  <p>This is what I've got this far:</p>
  
  <pre><code># change extension of all .out files:
  outFiles &lt;- dir(cntPath, "^.+\\.out", full.names = TRUE, ignore.case = TRUE, all.files = TRUE)
  for (nOut in outFiles){
    newOut &lt;- gsub('.out', '.csv', outFiles) # rename files to avoid accidental re-processing
    file.rename(outFiles, newOut)
  }
  # for each file, eliminate initial spaces, and replace consecutive spaces with a comma, for all rows and columns.
  t1 &lt;- read.csv(paste(cntPath, "test2.csv", sep = "/"), header = TRUE, sep = "\t", blank.lines.skip = FALSE)
  for (i in 1:length(nrow(t1))) {
    sub("^\\s+", "^\\S", i)
    sub("\\s+", ",", i)
  }
  </code></pre>
  
  <p>It should be relatively easy, but I am at lost of where I went wrong.
  Any help/suggestions would be appretiated.
  Thanks</p>