title: Simplifying repetitive code in R - how to avoid copy-pasting?
body: <p>I am learning how to use loops and functions to avoid copy-pasting code. I'm having a hard time with this example though; how would you reduce this?</p>
  <pre><code>  train$Name &lt;- replace(train$Name, train$Name == &quot;crop&quot;, 1)
    train$Name &lt;- replace(train$Name, train$Name == &quot;pasture&quot;, 2)
    train$Name &lt;- replace(train$Name, train$Name == &quot;secondary forest&quot;, 3)
    train$Name &lt;- replace(train$Name, train$Name == &quot;mature forest&quot;, 4)
  
  </code></pre>
  <p>#edit:
  I also have another part of code with a similar issue:</p>
  <pre><code>  bands &lt;- c('B1', 'B2', 'B3', 'B4', 'B5', 'B7')
    
    filepaths &lt;- paste0('./raw_data/LE07_L2SP_012054_20100211_20200911_02_T1/LE07_L2SP_012054_20100211_20200911_02_T1_', 'SR_', bands, '.TIF')
    bandlist &lt;- lapply(filepaths, raster)
    ...
    landsat &lt;- bandlist %&gt;% reduce(inner_join)
    saveRDS(landsat, 'landsat_2010.rds')
  </code></pre>
  <p>for example, here I want to go over several files, process them and save them as new dataframes named after their respective years. Is there a way to reduce this - naming each file with its corresponding year?</p>