title: Conditional Cross tabulation in R
body: <p>Looking for the quickest way to achieve below task using "expss" package.</p>
  
  <p>With a great package of "expss", we can easily do cross tabulation (which has other advantage and useful functions for cross-tabulations.), we can cross-tabulate multiple variables easily like below.</p>
  
  <pre><code> #install.packages("expss")
  
   library("expss")
   data(mtcars)
  
  
    var1 &lt;- "vs, am, gear, carb"
    var_names = trimws(unlist(strsplit(var1, split = ","))) 
  
  
    mtcars %&gt;%
      tab_prepend_values %&gt;%
      tab_cols(total(), ..[(var_names)]) %&gt;%
      tab_cells(cyl) %&gt;%
      tab_stat_cpct() %&gt;%
      tab_pivot()
  </code></pre>
  
  <p>Above gives an output as: (column %)</p>
  
  <pre><code>                      #Total    vs          am          gear            carb                        
                                  0     1     0     1     3     4   5     1   2    3   4    6    8 
  
    cyl             4    34.4   5.6  71.4  15.8  61.5   6.7  66.7  40  71.4  60                    
                    6    21.9  16.7  28.6  21.1  23.1  13.3  33.3  20  28.6           40  100      
                    8    43.8  77.8        63.2  15.4  80.0        40        40  100  60       100 
         #Total cases    32.0  18.0  14.0  19.0  13.0  15.0  12.0   5   7.0  10    3  10    1    1 
  </code></pre>
  
  <p>However, looking for an approach to create a table like below:</p>
  
  <pre><code> CYL    |  VS = 0   |  AM = 1   |   Gear = 4 or Gear = 5    |  Carb (All)
     4        5.56        61.54               58.82                34.38
     6        16.67       23.08               29.41                21.88
     8        77.78       15.38               11.76                43.75
  
  Total(col%) 100.00      100.00              100.00               100.00
  </code></pre>
  
  <p>Though i can achive this using dplyr and join functions but that is too complex incase we have to pass variables in runtime or dynamically.</p>
  
  <p>Any help will be appriciable. Thanks!!</p>