title: Problem adding computed columns in `group_by()` Error in dplyr in R
body: <p><a href="https://i.stack.imgur.com/t5s2X.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t5s2X.png" alt="enter image description here" /></a></p>
  <p>As shown in image, i have a .csv file. I am trying to create charts. I pass dataframe from this image to a function multi_bar(dt)</p>
  <p>The code uses a FOR LOOP and take column 1 (PS) and column 2 (APS1) in one go and then using column 1 (PS) and then column 2(APS2) by using for loop and dplyr. Basically I need column 1 in each iteration but the other column will keep changing from 2,3 and so on. The code here doesn't show code for chart but the error is coming when the code reaches column 3 (i=3) in for loop iteration. It works for i=2.</p>
  <pre><code>multi_bar=function(dt)
  {
    library(dplyr)
    library(ggplot2)
    for(i in 2:ncol(dt))
    {
      col1 = names(dt)[1]
      col2=names(dt[i])
      
      #df=select(x,x[,1], x[,i])
      df=dt[,c(1,i)]
      df1=df%&gt;%
        group_by(df[,1], df[,i])%&gt;%
        summarize(n=n())
      
      colnames(df1) &lt;- c(col1,col2,'Count')
      col3=names(df1)[3]
     
      ls=list(df1,col1,col2,col3,ncol(dt))
      
  }  
      
    return(ls)     
  }
  </code></pre>
  <p>when I pass dataframe to this function multi_bar, as shown in image (after reading csv file), the following error occurs</p>
  <p>Error: Problem adding computed columns in <code>group_by()</code>.
  x Problem with <code>mutate()</code> input <code>..2</code>.
  i <code>..2 = df[, i]</code>.
  x undefined columns selected
  Run <code>rlang::last_error()</code> to see where the error occurred.
  In addition: Warning message:
  Error: Problem adding computed columns in <code>group_by()</code>.
  x Problem with <code>mutate()</code> input <code>..2</code>.
  i <code>..2 = df[, i]</code>.
  x undefined columns selected
  Run <code>rlang::last_error()</code> to see where the error occurred.</p>