title: How can I wrap lines of code into an function that I can run with one command in R?
body: <p>So I'm working in a script and doing multiple tasks with the same sets of data. But because each task takes a few hundred lines of code, I end up clearing my global environment so I can move onto the next task. Then I have to rerun the lines of code at the top of the script to import my data again and work on my next task. I want to just type a command that will automatically reimport the data once I'm done with one task and can work on the other.</p>
  
  <p>Here is essentially what I run every time I need to work on the next task. I import my data with the read.csv function and then filter by certain rows I need. </p>
  
  <pre><code>d2015 &lt;- read_csv("Data 2015 CSV.csv")
  d2016  &lt;- read_csv("Data 2016 CSV.csv")
  d2017 &lt;- read_csv("Data 2017 CSV.csv")
  d2018 &lt;- read_csv("Data 2018 CSV.csv")
  
  dta_15 &lt;- d2015 %&gt;% filter(`Number` %in% c("TX-500", "TX-600", "TX-503", "TX-700", "TX-603", 
                                                     "AZ-502", "MI-501", "LA-503", "GA-500", "FL-510"))
  dta_16 &lt;- d2016 %&gt;% filter(`Number` %in% c("TX-500", "TX-600", "TX-503", "TX-700", "TX-603", 
                                                     "AZ-502", "MI-501", "LA-503", "GA-500", "FL-510"))
  dta_17 &lt;- d2017 %&gt;% filter(`Number` %in% c("TX-500", "TX-600", "TX-503", "TX-700", "TX-603", 
                                                     "AZ-502", "MI-501", "LA-503", "GA-500", "FL-510"))
  dta_18 &lt;- d2018 %&gt;% filter(`Number` %in% c("TX-500", "TX-600", "TX-503", "TX-700", "TX-603", 
                                                     "AZ-502", "MI-501", "LA-503", "GA-500", "FL-510"))
  
  </code></pre>
  
  <p>I tried putting it all in a loop but that didn't work,</p>
  
  <pre><code>rundata &lt;- {
  d2015 &lt;- read_csv("Data 2015 CSV.csv")
  d2016  &lt;- read_csv("Data 2016 CSV.csv")
  d2017 &lt;- read_csv("Data 2017 CSV.csv")
  d2018 &lt;- read_csv("Data 2018 CSV.csv")
  
  dta_15 &lt;- d2015 %&gt;% filter(`Number` %in% c("TX-500", "TX-600", "TX-503", "TX-700", "TX-603", 
                                                     "AZ-502", "MI-501", "LA-503", "GA-500", "FL-510"))
  dta_16 &lt;- d2016 %&gt;% filter(`Number` %in% c("TX-500", "TX-600", "TX-503", "TX-700", "TX-603", 
                                                     "AZ-502", "MI-501", "LA-503", "GA-500", "FL-510"))
  dta_17 &lt;- d2017 %&gt;% filter(`Number` %in% c("TX-500", "TX-600", "TX-503", "TX-700", "TX-603", 
                                                     "AZ-502", "MI-501", "LA-503", "GA-500", "FL-510"))
  dta_18 &lt;- d2018 %&gt;% filter(`Number` %in% c("TX-500", "TX-600", "TX-503", "TX-700", "TX-603", 
                                                     "AZ-502", "MI-501", "LA-503", "GA-500", "FL-510"))
  }
  </code></pre>
  
  <p>How  can i create one command that will rerun all these commands? Best.</p>