title: Pass a variable to dplyr::count in a loop
body: <p>I’m trying to run <code>dplyr::count()</code> on an arbitrary set of variables in one dataset. If I manually run <code>count()</code> once for each variable, I get the expected results. But when I try to put <code>count()</code> in a for loop to run it automatically for each variable in a set of variables, I got an error. It seems like the problem is in how I am passing the variable to <code>count()</code> within the for loop. I know that <code>count()</code> takes its variables unquoted, and for whatever reason R cannot tell that what I am passing it is a variable. </p>
  
  <p>I’ve tried a number of things to fix this, including passing the variables as <code>data$var1</code>, <code>quo(var1)</code>, <code>enquo(var1)</code>, <code>var1</code>, <code>“var1”</code>, <code>quo(data$var1)</code>, and <code>enquo(data$var1)</code> as well as unquoting the iterator with <code>!!</code>. I also tried specifying the arguments to <code>count()</code> like <code>count(x=data, var=i)</code>, but this caused <code>count()</code> to return the total number of rows in data as the count for each iteration. If you have any ideas about what is causing the error or how I can fix it, I would very much appreciate hearing them!</p>
  
  <p>Here is a minimal reproducible example that relies on the <code>lakers</code> dataset included with <code>lubridate</code>. </p>
  
  <pre><code># This code requires some of the packages in tidyverse. 
  library(dplyr)
  library(lubridate)
  
  
  # results = empty data frame for filling with info from the count() command
  results &lt;- data.frame()
  
  # mydata = the source data
  myData &lt;- lakers
  
  # myCols = list of the names of columns I want to count()
  myCols &lt;- c("opponent", "game_type", "player", "period")
  
  
  # Loop to count() every column in myCols automatically and store the results in 
  # one giant tibble of vars (var) and counts (n)
  
  for(i in myCols){
  results &lt;- bind_rows(results, count(x=myData, i))
  }
  </code></pre>