Search code examples
rmargintabular

Adding margin totals to table created with R package "tables"


The following code snippet creates a simple 2x2 contingency table using the "tables" package in R.

require(tables)
TAB <- latex(tabular((Gears=factor(gear)) ~  (Carburetors=carb)*(Format(digits=2)*
Percent("col")+ 1), data=mtcars))

What would be the easiest way of adding row and column totals to this table?


Solution

  • You already are showing how to do it for row totals (which is what the +1 is doing) and using the same method for the columns is effective:

    tabular((Gears=factor(gear)) +1~  (Carburetors=carb)*(Format(digits=2)*
            Percent("col")+ 1), data=mtcars)
    
           Carburetors    
     Gears Percent     All
     3      47         15 
     4      38         12 
     5      16          5 
     All   100         32