title: determining frequency of multiple variables based on multiple factors in R
body: <p>Suppose I have a dataset like this:</p>
  
  <pre><code>id &lt;- c(1,1,1,2,2,3,3,4,4)
  visit &lt;- c("A", "B", "C", "A", "B", "A", "C", "A", "B")
  test1 &lt;- c(12,16, NA, 11, 15,NA, 0,12, 5)
  test2 &lt;- c(1,NA, 2, 2, 2,2, NA,NA, NA)
  df &lt;- data.frame(id,visit,test1,test2)
  </code></pre>
  
  <p>I want to know the number of data points  per visit PER test so that the final output looks something like this:</p>
  
  <pre><code>visit   test1    test2
  A       3        3
  B       3        1
  C       1        1
  </code></pre>
  
  <p>I know I can use the aggregate function like this for 1 variable as mentioned on this <a href="https://stackoverflow.com/questions/43793024/frequency-of-data-points-by-two-variables-in-r">older post</a> :</p>
  
  <pre><code> aggregate(x = df$id[!is.na(df$test)], by = list(df$visit[!is.na(df$test)]), FUN = length)
  </code></pre>
  
  <p>but how would I go about doing this for multiple tests?</p>