title: In R - one row of data to be added to multiple files, iteratively
body: <p>I have a folder called &quot;HUCs&quot; with 2100 files, each named with an acronym and a number. There are three columns of data in each file, identical in length and with identical headers. I have another file called &quot;means&quot; with 2100 <code>rows</code>. Each <code>row</code> corresponds to one <code>file</code> in the HUCs folder, and is named accordingly.</p>
  <p>For example, row A2 in &quot;means&quot; contains &quot;1010001&quot; (its name), and B2 contains the means data. The &quot;1010001&quot; row data corresponds to a file in the HUCs folder called &quot;mricgcm_1010001&quot;.</p>
  <p>I need to somehow copy each row from the &quot;means&quot; file one at a time and place the data (three cells in the row) into its corresponding file in a new range of columns. So, the current data in the HUCs file are in columns A/B/C and the new &quot;means&quot; data can be placed into D/E/F (or some delimited equivalent).</p>
  <p>If there were only the one folder of 2100 files, I'd consider doing it by hand. But I have several similar folders. I can't even wrap my head around how to go about this. Is this possible in R? I tried my best, but please ask if this needs further explanation.</p>
  <p>This is the beginning of the list of HUC files:</p>
  <pre><code>mricgcm_1010002.csv
  mricgcm_1010003.csv
  mricgcm_1030001.csv
  mricgcm_1040001.csv
  mricgcm_1040002.csv
  mricgcm_1050001.csv
  mricgcm_1050002.csv
  mricgcm_1070002.csv
  mricgcm_1080107.csv
  mricgcm_1080204.csv
  mricgcm_1090003.csv
  mricgcm_1100001.csv
  mricgcm_1100002.csv
  mricgcm_1100003.csv
  mricgcm_1200001.csv
  </code></pre>
  <p>This is the worksheet in the &quot;means&quot; file:</p>
  <pre><code>HUC     Means
  1010002 141.0727273
  1010003 148.6072727
  1030001 158.3327273
  1040001 181.1127273
  1040002 222.78
  1050001 163.4818182
  1050002 231.9272727
  1070002 201.5018182
  1080107 183.0545455
  1080204 197.3836364
  1090003 209.14
  1100001 193.0472727
  1100002 186.1963636
  1100003 199.2854545
  1200001 1.021818182
  </code></pre>
  <p>This is what the HUCs files contain now:</p>
  <pre><code>HUC             Year    Predicted
  mricgcm_1010002 1961    10.8
  mricgcm_1010002 1962    4.6
  mricgcm_1010002 1963    0.8
  mricgcm_1010002 1964    66.3
  mricgcm_1010002 1965    20.4
  mricgcm_1010002 1966    8.5
  mricgcm_1010002 1967    13.5
  mricgcm_1010002 1968    3.7
  mricgcm_1010002 1969    59.2
  mricgcm_1010002 1970    13.8
  mricgcm_1010002 1971    5.5
  mricgcm_1010002 1972    1.9
  mricgcm_1010002 1973    9.9
  mricgcm_1010002 1974    11.7
  mricgcm_1010002 1975    52.4
  </code></pre>
  <p>And this is and example what I would like as a result, for each file:</p>
  <pre><code>HUC             Year    Predicted HUC       Mean
  mricgcm_1010002 1961    10.8      1010002   141.0727273
  mricgcm_1010002 1962    4.6                 141.0727273
  mricgcm_1010002 1963    0.8                 141.0727273
  mricgcm_1010002 1964    66.3                141.0727273
  mricgcm_1010002 1965    20.4                141.0727273
  mricgcm_1010002 1966    8.5                 141.0727273
  mricgcm_1010002 1967    13.5                141.0727273
  mricgcm_1010002 1968    3.7                 141.0727273
  mricgcm_1010002 1969    59.2                141.0727273
  mricgcm_1010002 1970    13.8                141.0727273
  mricgcm_1010002 1971    5.5                 141.0727273
  mricgcm_1010002 1972    1.9                 141.0727273
  mricgcm_1010002 1973    9.9                 141.0727273
  mricgcm_1010002 1974    11.7                141.0727273
  mricgcm_1010002 1975    52.4                141.0727273
  </code></pre>
  <p>Note, FTR: The mean is NOT the mean of the column of <em>predicted</em> data or else I could just run a mean formula on that column. It is the mean of the historic data, gotten from elsewhere.</p>
  <p>This is what I am getting now:</p>
  <pre><code>Designation HUC YEAR    RO_MM   HIST    HUC Historic Mean   D
  mricgcm 1010004 1961    71.8                
  mricgcm 1010004 1962    119             
  mricgcm 1010004 1963    177.7               
  mricgcm 1010004 1964    166             
  mricgcm 1010004 1965    159             
  mricgcm 1010004 1966    180.3
  </code></pre>
  <p>So, something is happening, but no data.</p>