title: Iterating across sequences of columns (of same crop-related data) to build tidy rbind dataset in R
body: <p>I am working with a messy wide format dataset of CROP data where each crop has six associated variables (crop type, crop acreage, nitrogen in soil, nitrogen applied, organic v. conventional, and notes). There are 50 sets of these 6 variables, 300 columns total, with the unfortunate naming convention: c.0. | a.0. | s.0. | f.0. | o.0. | r.0. |, .... , | c.23. | a.23 | s.23. | f.23. |o.23. | r.23. |.</p>
  <p>I want to iterate across sequences of columns (the 6 columns associated with each crop), saving each iteration, or individual crop, as a list (or df) with the common colnames c(CROP, CROP_ACREAGE, SOIL_N, APP_N, TYPE, NOTES). Once all of the crops have been iterated through, I want to build a tidy df by rbinding the 50 individual crops datasets.</p>
  <p>Below is an example dataset with only two sets of crop columns, four growers, across three years:</p>
  <pre><code>data &lt;- tribble(
  ~grower, ~ YEAR, ~c.0., ~a.0., ~s.0., ~f.0., ~o.0., ~r.0., ~c.10., ~a.10, ~s.10., ~f.10., ~o.10., ~r.10.,
  &quot;Bob&quot;, 2014, &quot;Kale, Baby&quot;, 7.0, 87.0, 126.0, &quot;C&quot;, &quot;&quot;, &quot;Carrot&quot;, 16.0, 47.8, 137.0, &quot;O&quot;, &quot;&quot;,
  &quot;Janet&quot;, 2015, &quot;Broccoli&quot;, 18.0, 68.2,162.0, &quot;O&quot;, &quot;&quot;, &quot;Garlic&quot;, 25.0, 9.1, 152.3, &quot;C&quot;, &quot;&quot;,
  &quot;Chris&quot;, 2014, &quot;Cabbage&quot;, 34.2, 8.6, 200.7, &quot;C&quot;, &quot;&quot;, &quot;Cauliflower&quot;, 105.2, 113.0, 199.4, &quot;O&quot;, &quot;&quot;,
  &quot;Ted&quot;, 2016, &quot;Kale&quot;, 12.2, 11.9, 120.2, &quot;C&quot;, &quot;&quot;, &quot;Lettuce, Head&quot;, 55.2, 113.0, 166.5, &quot;C&quot;, &quot;NY&quot;
  )
  </code></pre>
  <p>I've built code that will work... but that will be SUPER inefficient:</p>
  <pre><code>build &lt;- function(choice, iter){
    
    a &lt;- data %&gt;% select(choice) %&gt;% 
    rename(CROP = 3,
           ACREAGE = 4,
           N_SOIL = 5,
           N_APPLIED = 6,
           O_C = 7,
           NOTES = 8)
    
    saveRDS(a, file = paste0(&quot;./intermediate-data/&quot;,iter,&quot;.RDS&quot;))
  
  }
  
  build(choice = c(1:8), iter = &quot;crop1&quot;)
  build(choice = c(1:2,9:14), iter = &quot;crop2&quot;)
  # use build() function until all 50 crops have been built into separate df's
  </code></pre>
  <p>Then I'd pull these all in from the intermediate-data directory and rbind().</p>
  <p>But I know there has got to be a more efficient way... where I can iterate across sets of 6-crop-columns without specifying them directly. Any ideas?</p>