title: reading in tiff files and matching them based on name
body: <p>I am using R 4.2.0 and am trying to read a series on tif files nested in different folders. My folder set up is as follows:</p>
  <p>scenario 1/ global_model/ high_level/</p>
  <pre><code>         scenario1_highlevel_2020.tif
           scenario1_highlevel_2030.tif
           scenario1_highlevel_2040.tif
  </code></pre>
  <p>scenario 1/ global_model/nested/</p>
  <pre><code>         scenario1_nested_2020.tif
           scenario1_nested_2030.tif
           scenario1_nested_2040.tif
  </code></pre>
  <p>scenario 2/ global_model/ high_level/</p>
  <pre><code>         scenario2_highlevel_2020.tif
           scenario2_highlevel_2030.tif
           scenario2_highlevel_2040.tif
  </code></pre>
  <p>scenario 2/ global_model/nested/</p>
  <pre><code>         scenario2_nested_2020.tif
           scenario2_nested_2030.tif
           scenario2_nested_2040.tif
  </code></pre>
  <p>Essentially what I am trying to do is to merge the nested and highlevel tifs from each scenario and each year. For example, I am trying to merge <em>scenario1_highlevel_2020.tif</em> and <em>scenario2_nested_2020.tif</em>. I have all the necessary code to merge each one individually but I am running into issues how to loop through each scenario folder.</p>
  <p>What I would like to do is loop through scenario 1 folder, read in <em>scenario1_highlevel_2020.tif</em>
  and <em>scenario1_highlevel_2020.tif</em> and then merge them, then go onto read in <em>scenario1_highlevel_2030.tif</em>
  and <em>scenario1_highlevel_2030.tif</em>  and merge those, and then read in <em>scenario1_highlevel_2030.tif</em>
  and <em>scenario1_highlevel_2030.tif</em> and merge those. Then I want to loop through scenario 2 and do the same.</p>
  <pre><code># What I am thinking right now (in English code)
  for each tif in scenario1/global_model/nested {
       read in tif
  } # this would give me 3 tiffs, nested_2020, nested_2030, nested_2040
  
  for each tif in scenario 1/global_model/high level {
       read in tif
  } #this would give me 3 tiffs, highlevel_2020, highlevel_2030, highlevel_2040
  
  for each tif in (2020,2030,2040) {
       use function I created to merge the tifs}
  </code></pre>
  <p>Then I need to do the same for scenario 2, which could probably be done earlier in the code?</p>
  <p>Im fairly new to R/ havent used it a few years so please let me know if you have any ideas of how to do this!! Thank you!!!</p>