title: Using "for" loops in R package and error message "Error in `[.data.frame`(, c("date", i)) : undefined columns selected"
body: <p>Question relates to using &quot;for&quot; loops in R package and handling error message&quot;undefined columns.</p>
  <p>Sample date frame consists of 4 rows and 10 columns. Libraries used magritter and dplyr.</p>
  <pre><code>library(magrittr)
  library(dplyr)
  
  date &lt;- c (&quot;2020-12-31&quot;, &quot;2021-01-29&quot;, &quot;2021-02-26&quot;, &quot;2021-03-31&quot;)
  v_1 &lt;- c(66.39, 70.46, 69.37, 67.73) 
  v_2 &lt;- c(14.16, 12.31, 13.07, 12.85) 
  v_3 &lt;- c(14.37, 13.02, 13.09, 14.88) 
  v_4 &lt;- c(0.94, 0.83, 0.74, 0.79) 
  v_5 &lt;- c(0.42, 0.37, 0.33, 0.29) 
  v_6 &lt;- c(1.51, 1.30, 1.14, 1.12) 
  v_7 &lt;- c(1.84, 1.37, 1.91, 1.99) 
  v_8 &lt;- c(0.16, 0.14, 0.14, 0.14)
  v_9 &lt;- c(0.21, 0.20, 0.21, 0.21) 
  
  raw.data &lt;- data.frame(date, v_1, v_2, v_3, v_4, v_5, v_6, v_7, v_8, v_9)
  </code></pre>
  <p>The variables v_1 through v_9 are each assigned a priority, namely &quot;High&quot;, &quot;Medium&quot; or &quot;Low&quot;</p>
  <pre><code>Priority &lt;- c(&quot;High&quot;, &quot;Medium&quot;, &quot;Low&quot;)
  
    High &lt;- c(&quot;v_4&quot;, &quot;v_7&quot;)
    Medium &lt;- c(&quot;v_2&quot;, &quot;v_3&quot;, &quot;v_9&quot;)
    Low &lt;- c(&quot;v_1&quot;, &quot;v_5&quot;, &quot;v_6&quot;, &quot;v_8&quot;)
    
  </code></pre>
  <p>Required output in tidy/long format to contain 12 rows and 3 columns.</p>
  <p>12 Rows: date variable that is repeated thrice.</p>
  <p>3 columns: date, Priority and Priority Total. (Priority Total is the row wise sum of the variables falling under each priority category)</p>
  <pre><code>      date        Priority    Priority_Total
    1  2020-12-31     High           2.78
    2  2021-01-29     High           2.20
    3  2021-02-26     High           2.65
    4  2021-03-31     High           2.78
    5  2020-12-31   Medium          28.74
    6  2021-01-29   Medium          25.53
    7  2021-02-26   Medium          26.37
    8  2021-03-31   Medium          27.94
    9  2020-12-31      Low          68.48
    10 2021-01-29      Low          72.27
    11 2021-02-26      Low          70.98
    12 2021-03-31      Low          69.28
  </code></pre>
  <p>Required output obtained successfully using the paste and copy method.</p>
  <p>Note that &quot;tmp&quot; is a temporary work space.</p>
  <pre><code>  pc_output &lt;- NULL # Required output initialized
    
  
     # High Priority
      
      tmp &lt;- raw.data[,c(&quot;date&quot;,High)] %&gt;% # Selecting date and high priority variables. 
      
      mutate(Priority_Total=select(.,-1) %&gt;% # Generating a priority total by summing across the rows.
              apply(1,sum,na.rm=TRUE)) %&gt;% 
              mutate(Priority=&quot;High&quot;) # Generating a priority category column containing value &quot;High&quot;
    
      tmp &lt;- tmp[,c(&quot;date&quot;,&quot;Priority&quot;,&quot;Priority_Total&quot;)] # Retaining the 3 required columns
      
      pc_output &lt;- rbind(pc_output,tmp) # Merging the derived result with pc_output 
    
  # The same procedure is used for medium and low priority variables
    
      # Medium Priority 
      tmp &lt;- raw.data[,c(&quot;date&quot;,Medium)] %&gt;% # Selecting date and medium priority variables. 
        
        mutate(Priority_Total=select(.,-1) %&gt;% # Generating a priority total by summing across the rows.
                 apply(1,sum,na.rm=TRUE)) %&gt;% 
        mutate(Priority=&quot;Medium&quot;) # Generating a priority category column containing value &quot;Medium&quot;
      
      tmp &lt;- tmp[,c(&quot;date&quot;,&quot;Priority&quot;,&quot;Priority_Total&quot;)] # Retaining the 3 required columns
      
      pc_output &lt;- rbind(pc_output,tmp) # Merging the derived result with pc_output 
      
  
      # Low Priority 
      tmp &lt;- raw.data[,c(&quot;date&quot;,Low)] %&gt;% # Selecting date and low priority variables. 
        mutate(Priority_Total=select(.,-1) %&gt;% # Generating a priority total by summing across the rows.
        apply(1,sum,na.rm=TRUE)) %&gt;% 
        mutate(Priority=&quot;Low&quot;) # Generating a priority category column containing value &quot;Low&quot;
      
      tmp &lt;- tmp[,c(&quot;date&quot;,&quot;Priority&quot;,&quot;Priority_Total&quot;)] # Retaining the 3 required columns
      
      pc_output &lt;- rbind(pc_output,tmp) # Merging the derived result with pc_output
      
      rm(tmp) # Clearing the temporary work space
  
      print(pc_output)
  </code></pre>
  <p>The &quot;paste and copy&quot; method was run to ensure that all the columns were properly defined.</p>
  <p>However when the &quot;for&quot; loop was used, the &quot;undefined columns&quot; problem was encountered.</p>
  <pre><code>    output &lt;- NULL
  
      for (i in (Priority)){
        tmp &lt;- raw.data[,c(&quot;date&quot;,i)] %&gt;% 
        mutate(Priority_Total=select(.,-1) %&gt;%
            apply(1,sum,na.rm=TRUE)) %&gt;%
            mutate(Priority=i)
  
        tmp &lt;- tmp[,c(&quot;date&quot;,&quot;Priority&quot;,&quot;Priority_Total&quot;)]
  
    output &lt;- rbind(output,tmp)
    }
  </code></pre>
  <p>Message : &quot;Error in <code>[.data.frame</code>(raw.data, , c(&quot;date&quot;, i)) : undefined columns selected&quot;</p>