title: Summarizing using dplyr with a for loop
body: <p>I would like to summarise each of my independant variables (columns) with my target variable using <code>dplyr</code> over a for loop. This is my main dataframe:</p>
  
  <pre>  contract_ID    Asurion         Variable_1     Variable_2  Variable_3
           1          Y                a               c          f
           2          Y                a               d          g
           3          N                b               c          g
           4          N                a               d          f
           5          Y                b               c          f
           6          Y                a               d          f
  </pre>
  
  <p>After the group by I get </p>
  
  <pre><code>a1 &lt;- a %&gt;% 
    group_by(Asurion,BhvrBnk_Donates_to_Env_Causes) %&gt;%       
    summarise(counT=n_distinct(CONTRACT_ID)) %&gt;%                                        
    mutate(perc=paste0(round(counT/sum(counT)*100,2),"%"))
  
   Asurion Variable_1 CounT   perc
      Y         a        3     75%
      Y         b        1     25%
      N         a        1     50%
      N         b        1     50%
  </code></pre>
  
  <p>I would like to have this summairsation for each of my variable present in my dataframe and I would like to do this using a for loop. How do i get to my desired result </p>
  
  <p>This is what I have tried using but it doesnt seem to work. it is for a school project and I need to use a for loop for this. Please help me out here </p>
  
  <pre><code>categorical &lt;- colnames(a)###where categroical is the names of all columns in a  
  ###I would like to have a for loop for every column in a and summarise in the following way. I would like to store each of the summarisations in a separate dataframe 
  
  for (i in categorical) {
    a[[i]] &lt;- a %&gt;% 
       group_by(Asurion,get(i)) %&gt;% 
      summarise(counT=n_distinct(CONTRACT_ID)) %&gt;% 
      mutate(perc=paste0(round(counT/sum(counT)*100,2),"%"))
    }
  </code></pre>