percents: Row and Column Percentages

percentsR Documentation

Row and Column Percentages

Description

Calculate percentages in a table. rowPercent gives row percentages, colPercent gives column percentages, and rowColPercent gives both row and column percentages.

Usage

colPercent(t, pretty.text = FALSE, keep = TRUE, digits = 4)

rowPercent(t, pretty.text = FALSE, keep = TRUE, digits = 4)

rowColPercent(t, keep = TRUE, ...)

colPercentAsHTML(
  t,
  keep = TRUE,
  row.names = NULL,
  column.names = NULL,
  html.table.border = 0,
  banded.rows = FALSE,
  col.odd = "none",
  col.even = "lightgrey",
  caption = NA,
  transpose = FALSE,
  ...
)

rowPercentAsHTML(
  t,
  keep = TRUE,
  row.names = NULL,
  column.names = NULL,
  html.table.border = 0,
  banded.rows = FALSE,
  col.odd = "none",
  col.even = "lightgrey",
  caption = NA,
  transpose = FALSE,
  ...
)

rowColPercentAsHTML(
  t,
  keep = TRUE,
  row.names = NULL,
  column.names = NULL,
  html.table.border = 0,
  banded.rows = FALSE,
  col.odd = "none",
  col.even = "lightgrey",
  caption = NA,
  ...
)

Arguments

t

a matrix

pretty.text

logical; if TRUE, will format the table into nice display

keep

logical; if TRUE, the original table counts will be kept along with the percentages

digits

number of digits to round to

...

additional arguments from colPercent and rowPercent for rowColPercent, or additional arguments from non-HTML functions to HTML functions.

row.names

(optional) vector of row names in table

column.names

(optional) vector of column names in table

html.table.border

border type for the table. Defaults to 0 in HTML syntax.

banded.rows

logical; if TRUE, alternating rows will have different shaded colours.

col.odd

colour to use for odd numbered rows

col.even

colour to use for even numbered rows

caption

table caption. Uses addTableNumber() to increment table number

transpose

logical; if TRUE, the table is transposed.

Details

Generates a table of row and/or column percentages given table t. Using pretty.text = TRUE will add the \

Row and column names can be replaced by row.names and column.names. Higher values of html.table.border make the table borders thicker and even look 3D. col.odd and col.even are ignored when banded.rows = FALSE.

Transposing is useful if there are many columns in a single row or vice versa. Note that the percentages are unchanged, only the orientation of the table.

Value

A table with row-wise/column-wise percentages added. The percentages sum to 1 per row/column.

HTML-based functions return the code used to generate a table that shows row and/or column percentages.

Author(s)

Aline Talhouk, Samuel Leung, Derek Chiu

Examples

# Base outputs
A <- matrix(c(2, 3, 5, 10), nrow = 2, dimnames = list(c("Row1", "Row2"), c("Col1", "Col2")))
rowPercent(A)
rowPercent(A, keep = FALSE)
colPercent(A, pretty.text = TRUE)
colPercent(A, pretty.text = TRUE, keep = FALSE)
rowColPercent(A, digits = 2)

# HTML outputs
library(htmlTable)
set.seed(13)
B <- matrix(rbinom(16, size = 20, prob = 0.3), nrow = 4,
dimnames = list(paste0("Row", 1:4), paste0("Col", 1:4)))
htmlTable(rowColPercentAsHTML(B, keep = TRUE, digits = 2, pretty.text = TRUE,
banded.rows = TRUE, col.odd = "yellow", col.even = "green", caption =
"Example Table", html.table.border = 2))

TalhoukLab/biostatUtil documentation built on April 14, 2025, 4:15 a.m.