title: How count specific occurence of a variable through a conditional if-statement loop
body: <p>I am trying to count the occurrences of some observations according to the value taken by the 'unimodal_preset' variable (which you might find in the dataset I let you attached below) and store them in a small table, built as follows:</p>
  <pre><code>tab &lt;- matrix(rep(0, times= 16), ncol= 2, byrow=TRUE)
  colnames(tab) &lt;- c('congruent', 'incongruent')
  rownames(tab) &lt;- rep(c('written', 'spoken'), 4)
  tab &lt;- as.table(tab)
  tab
  </code></pre>
  <p>I have prepared the following for loop:</p>
  <pre><code>for (i in rownames(tab)){
    for (j in colnames(tab)){
      
      if (di$unimodal_preset == 0 &amp; di$cong_cond == 'congreunt' &amp; di$presentation_mode == 'spoken'){
        tab[i,j] &lt;- di %&gt;% filter(cong_cond == 'congruent' &amp; presentation_mode == 'spoken' &amp; unimodal_preset == 0) %&gt;%  summarise(n = n()) %&gt;% as.data.frame()
        
      } else if (di$unimodal_preset == 0 &amp; di$cong_cond == 'congreunt' &amp; di$presentation_mode == 'written') {
        tab[i,j] &lt;-  di %&gt;% filter(cong_cond == 'congruent' &amp; presentation_mode == 'written' &amp; unimodal_preset == 0) %&gt;%  summarise(n = n()) %&gt;% as.data.frame()
        
        
      } else if (di$unimodal_preset == 0 &amp; di$cong_cond == 'incongreunt' &amp; di$presentation_mode == 'spoken') {
        tab[i,j] &lt;-  di %&gt;% filter(cong_cond == 'incongruent' &amp; presentation_mode == 'spoken' &amp; unimodal_preset == 0) %&gt;% summarise(n = n()) %&gt;% as.data.frame()
        
      } else if (di$unimodal_preset == 0 &amp; di$cong_cond == 'incongreunt' &amp; di$presentation_mode == 'written') {
        tab[i,j] &lt;-  di %&gt;% filter(cong_cond == 'incongruent' &amp; presentation_mode == 'written' &amp; unimodal_preset == 0) %&gt;%  summarise(n = n()) %&gt;% as.data.frame()
        
        
      } else if (di$unimodal_preset == 1 &amp; di$cong_cond == 'congreunt' &amp; di$presentation_mode == 'spoken') {
        tab[i,j] &lt;-  di %&gt;% filter(cong_cond == 'congruent' &amp; presentation_mode == 'spoken' &amp; unimodal_preset == 1) %&gt;%  summarise(n = n()) %&gt;% as.data.frame()
        
        
      } else if (di$unimodal_preset == 1 &amp; di$cong_cond == 'congreunt' &amp; di$presentation_mode == 'written') {
        tab[i,j] &lt;-  di %&gt;% filter(cong_cond == 'congruent' &amp; presentation_mode == 'written' &amp; unimodal_preset == 1) %&gt;%  summarise(n = n()) %&gt;% as.data.frame()
        
      } else if (di$unimodal_preset == 1 &amp; di$cong_cond == 'incongreunt' &amp; di$presentation_mode == 'spoken') {
        tab[i,j] &lt;-  di %&gt;% filter(cong_cond == 'incongruent' &amp; presentation_mode == 'spoken' &amp; unimodal_preset == 1) %&gt;%  summarise(n = n()) %&gt;% as.data.frame()
        
      } 
      
      else if  (di$unimodal == 1 &amp; di$cong_cond == 'incongreunt' &amp; di$presentation_mode == 'written'){
        tab[i,j] &lt;-  di %&gt;% filter(cong_cond == 'incongruent' &amp; presentation_mode == 'written' &amp; unimodal_preset == 1) %&gt;%  summarise(n = n())%&gt;% as.data.frame()
  
      }
      
    }
  }
  
  print(tab)
  </code></pre>
  <p>but unfortunately, it does not seem to work on my end. Could you please suggest a possible way through? Here is the dataset I have used</p>
  <pre><code>&gt; dput(head(di, 150))
  structure(list(cong_cond = c(&quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, 
  &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, 
  &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, 
  &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, 
  &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, 
  &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, 
  &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, 
  &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, 
  &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, 
  &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, &quot;incongruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, &quot;congruent&quot;, 
  &quot;congruent&quot;, &quot;congruent&quot;), presentation_mode = c(&quot;spoken&quot;, &quot;spoken&quot;, 
  &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, 
  &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, 
  &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, 
  &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, 
  &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;written&quot;, 
  &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, 
  &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, 
  &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, 
  &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, 
  &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, 
  &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;spoken&quot;, &quot;spoken&quot;, 
  &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, 
  &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, 
  &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, 
  &quot;spoken&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, 
  &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, 
  &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, 
  &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;, 
  &quot;written&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, 
  &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, 
  &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, 
  &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, 
  &quot;spoken&quot;, &quot;spoken&quot;, &quot;spoken&quot;, &quot;written&quot;, &quot;written&quot;, &quot;written&quot;
  ), unimodal_preset = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 
  1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -150L), class = c(&quot;tbl_df&quot;, 
  &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>