title: How to count number of observations in each column in a grouped dataframe in r
body: <p>I have a dataframe composed of geochemical samples result which includes the following variables:</p>
  
  <pre><code>Year, Zone, *48 analyzed elements*, *more information*.
  </code></pre>
  
  <p>I want to know how many samples were collected each year, in each zone for every elements. So basically, I would like a table that would look like this:</p>
  
  <pre><code>Year,Zone,Ag_ppm, ..., Zr_ppm
  1981, ZoneA, 0, ..., 0 
  1981, ZoneB, 20, ..., 0
  1983, ZoneA, 0, ..., 150 
  </code></pre>
  
  <p>I have tried the following:  </p>
  
  <pre><code> Elt_count &lt;- SoilGeology %&gt;%
    group_by(Year, Zone) %&gt;%
    summarise_at(vars(Ag_ppm:Zr_ppm),funs(sum)) %&gt;%
    select(Year, Zone, Ag_ppm:Zr_ppm)
  </code></pre>
  
  <p>It works but it does not give me the information I want (I don't want the cummulative sum of the samples , but a count of every samples).
  I have then tried:</p>
  
  <pre><code>Elt_count &lt;- SoilGeology %&gt;%
    group_by(Year, Zone) %&gt;%
    summarise_at(vars(Ag_ppm:Zr_ppm),funs(n)) %&gt;%
    select(Year, Zone, Ag_ppm:Zr_ppm)
  </code></pre>
  
  <p>But I get the following error: <code>Error in summarise_impl(.data, dots) :</code>n()<code>does not take arguments</code></p>
  
  <p>I have also tried:</p>
  
  <pre><code>d &lt;- SoilGeology %&gt;%
    group_by(Year, Zone) %&gt;%
    summarise_all(n) %&gt;%
    select(Year, Zone, Ag_ppm:Zr_ppm)
  </code></pre>
  
  <p>But I get the same error as above: <code>Error in summarise_impl(.data, dots) :</code>n()<code>does not take arguments</code></p>
  
  <p>And also tried with count:</p>
  
  <pre><code>Elt_count &lt;- SoilGeology %&gt;%
    group_by(Year, Zone) %&gt;%
    count(Au_ppm:Zr_ppm, na.rm = TRUE) %&gt;%
    select(Year, Zone, Ag_ppm:Zr_ppm)
  </code></pre>
  
  <p>But, I get the error: </p>
  
  <pre><code>Error in mutate_impl(.data, dots) : Evaluation error: NA/NaN argument.
  In addition: Warning messages:
  1: In Au_ppm:Zr_ppm :
    numerical expression has 52 elements: only the first used
  2: In Au_ppm:Zr_ppm :
    numerical expression has 52 elements: only the first used
  </code></pre>
  
  <p>Does someone has an explanation for these errors? Or a better solution for my problem?</p>
  
  <p>Thanks!</p>