title: Count number of non-NA values by group
body: <p>For example, I have this data frame (df):</p>
  <pre><code>Color    X1      X2    X3    X4
  Red      1       1     0     2
  Blue     0       NA    4     1 
  Red      3       4     3     1
  Green    2       2     1     0
  </code></pre>
  <p>I would like to create a function that counts up the number of non-<code>NA</code>s in &quot;X2&quot; by group (i.e. by &quot;color&quot;). I would like the output of this function in a new data frame named newdf. This is what I would like for output:</p>
  <pre><code>Color    X2     
  Red      2      
  Blue     NA    
  Green    1
  </code></pre>
  <p>So far, I have this code:</p>
  <pre><code>Question &lt;- function(Color){
    Result &lt;-
      rowsum((df[c(&quot;X2&quot;)] &gt; 0) + 0, df[[&quot;X2&quot;]], na.rm = TRUE) 
    rowSums(Result)[[Color]]
    }
    Question(&quot;Red&quot;) 
  </code></pre>
  <p>The output this function gives is just <code>Question(&quot;Red&quot;)= 2</code> and I would like to instead get my results of all the colors in a new data frame (newdf). Can anyone help with this? Thanks!</p>