kTable: Make Nicely Formatted Tables

Description Usage Arguments Examples

View source: R/misc.R

Description

Function for creating nice 1D and 2D tables. Tables are generated and formatted with both counts and percentages. Primarily intended to be used with R Markdown documents, calling some of the table printing functions. The function returns a data.frame in a format that can be used with utility HTML generation functions.

Usage

1
2
3
kTable(x, y = NULL, deparse.level = 2, top.left.cell = "",
  col.names = NULL, row.names = NULL, left.label = NULL,
  top.label = NULL, google = FALSE)

Arguments

x

the x variable to build a table on.

y

optional: the y variable to build a table on. Used for 2x2 contingency tables.

deparse.level

passed to table; deparse.level=2 allows us to pass through variable names.

top.left.cell

the string to set in the top left cell of the table.

col.names

a vector of column names to use on the outputted table; typically this is parsed from the variables passed through.

row.names

a vector of row names to use on the outputted table; typically this is parsed from the variables passed through.

left.label

the label to use for the rows; typically parsed from x. Only used for 2D tables (ie, when y is not null).

top.label

the label to use for the columns; typically parsed from y. Only used for 2D tables (ie, when y is not null).

google

used if you plan on passing the table to gvistable from the googleVis package.

Examples

1
2
3
4
5
6
7
x <- rbinom(100, size=2, p=0.1)
y <- rbinom(100, size=2, p=0.1)

## try these in an R markdown document for best results
kTable(x)
my_table <- kTable(x, y, top.left.cell="foo", left.label="bar", top.label="baz")
pxt( my_table )

Example output

          [,1]    [,2]        
          "x"     "Count (%)" 
          "0"     "81 (81.0%)"
          "1"     "18 (18.0%)"
          "2"     " 1 (1.00%)"
my_colSum "Total" "100"       
<table class='twoDtable' ><tr><td colspan=2 rowspan=2 >foo</td><td colspan=3 >baz</td><td ></td></tr><tr><td >0</td><td >1</td><td >2</td><td >Total</td></tr><tr><td rowspan=3 >bar</td><td >0</td><td >61 (77.2%)</td><td >19 (95.0%)</td><td >1 ( 100%)</td><td >81</td></tr><tr><td >1</td><td >17 (21.5%)</td><td > 1 (5.00%)</td><td >0 (0.00%)</td><td >18</td></tr><tr><td >2</td><td > 1 (1.26%)</td><td > 0 (0.00%)</td><td >0 (0.00%)</td><td >1</td></tr><tr><td ></td><td >Total</td><td >79</td><td >20</td><td >1</td><td >100</td></tr></table> 

Kmisc documentation built on May 29, 2017, 1:43 p.m.

Related to kTable in Kmisc...