tabler_by | R Documentation |
This function is helpful for making simple, formatted tables similar to
the functionality of tabular
.
tabler_by
creates simple tables, and tabler_by2
is a wrapper
which can create stratified tables (tabler_by
can also achieve this
but requires additional steps).
tabler_by(
data,
varname,
byvar,
n = NULL,
order = FALSE,
zeros = TRUE,
pct = FALSE,
pct.column = FALSE,
pct.total = FALSE,
pct.sign = FALSE,
drop = TRUE
)
tabler_by2(
data,
varname,
byvar,
n = NULL,
order = FALSE,
stratvar = NULL,
zeros = TRUE,
pct = FALSE,
pct.column = FALSE,
pct.total = FALSE,
pct.sign = TRUE,
drop = TRUE,
collapse_varname = FALSE,
collapse_format = c("<b>%s</b>", "%s")
)
data |
a data frame; variables |
varname |
subgroup variable name (rows) |
byvar |
stratification variable name (columns) |
n |
number in each group; see details |
order |
logical; order the result by decreasing frequency |
zeros |
optional character string replacement for cells which have
zero counts; will appear as |
pct |
logical; if |
pct.column |
logical; if |
pct.total |
logical; if |
pct.sign |
logical; if |
drop |
logical; for |
stratvar |
for |
collapse_varname |
logical; for |
collapse_format |
format strings for headers and normal rows,
respectively, passed to |
varname
and byvar
should be factors, and the levels will
appear in the output as they occur in levels(x)
.
n
is used to calculate the percent. If missing, the output will
only show counts in the table. If given, length(n)
should be one or
equal to the number of levels of byvar
.
If one n
is given, tabler_by
assumes that this is the total
population for a subgroup, e.g., if creating a table for a subset of the
data, it is only necessary to provide the total n
for that group.
If more than one n
is given, tabler_by
assumes that the
entire data set is given as data
and will use the corresponding
n
for percents.
tox_worst
; match_ctc
Other tabler:
tabler()
,
tabler_resp()
,
tabler_stat()
,
tabler_stat2()
mt <- within(mtcars, {
am <- factor(am)
gear <- factor(gear)
vs <- factor(vs, 0:2)
vs2 <- factor(vs, 2:0)
carb <- factor(carb)
})
tabler_by(mt, 'vs', 'gear')
tabler_by(mt, c('vs2', 'carb'), 'gear', order = FALSE)
tabler_by(mt, c('vs2', 'carb'), 'gear', order = TRUE)
## when length(n) > 1, each column uses a different n for percents
tabler_by(mt, 'vs', 'gear', n = 32, pct = TRUE)
tabler_by(mt, 'vs', 'gear', n = table(mt$gear), pct = TRUE)
tabler_by(mt, 'vs', 'gear', n = table(mt$gear), zeros = '-',
pct = TRUE, pct.column = TRUE, pct.total = TRUE)
## use tabler_by2 to create a stratified table
t1 <- tabler_by2(mt, c('vs', 'carb'), 'gear', stratvar = 'am', order = TRUE)
## or tabler_by to do this in several steps
t2 <- cbind(
tabler_by(mt, varname = c('vs', 'carb'), byvar = 'gear',
drop = FALSE)[, c('carb', 'Total')],
tabler_by(mt[mt$am == 0, ], varname = c('vs', 'carb'), byvar = 'gear',
drop = FALSE)[, -1],
tabler_by(mt[mt$am == 1, ], varname = c('vs', 'carb'), byvar = 'gear',
drop = FALSE)[, -1]
)
## order, drop extra rows/columns, set rownames
rownames(t2) <- locf(rownames(t2))
t2 <- t2[order(locf(rownames(t2)), -xtfrm(t2[, 2])), ]
t2 <- t2[!t2[, 'Total'] %in% '0', ]
t2 <- t2[, apply(t2, 2, function(x) !all(x %in% '0'))]
rownames(t2)[duplicated(rownames(t2))] <- ''
stopifnot(identical(t1, t2))
## example workflow
set.seed(1)
f <- function(x, ...) sample(x, 100, replace = TRUE, ...)
tox <- data.frame(
id = rep(1:10, 10), phase = 1:2,
code = f(rawr::ctcae_v4$tox_code[1:100]),
grade = f(1:3, prob = c(0.6, 0.3, 0.1))
)
tox <- cbind(tox, match_ctc(tox$code)[, c('tox_cat', 'tox_desc')])
## get worst toxicities by id, by grade
n <- colSums(table(tox$id, tox$phase) > 0)
tox[] <- lapply(tox, factor)
tox <- tox_worst(tox, desc = 'tox_desc')
out <- tabler_by2(tox, 'tox_desc', 'grade', stratvar = 'phase', zeros = '-')
colnames(out)[1] <- sprintf('Total<br /><font size=1>n = %s</font>', sum(n))
cgroup <- c(
'',
sprintf('Phase I<br /><font size=1>n = %s</font>', n[1]),
sprintf('Phase II<br /><font size=1>n = %s</font>', n[2])
)
htmlTable::htmlTable(
out, ctable = TRUE, cgroup = cgroup, n.cgroup = c(1, 4, 4),
caption = 'Table 1: Toxicities<sup>†</sup> by phase and grade,
sorted by total.',
col.columns = rep(c('grey97', 'none', 'grey97'), times = c(1, 4, 4)),
col.rgroup = rep(rep(c('none', 'grey97'), each = 5), 10),
tfoot = paste0('<font size=1><sup>†</sup>Percents represent ',
'proportion of patients out of respective phase total.</font>')
)
## same as above but add level of stratification, sort by total within group
out2 <- tabler_by2(tox, c('tox_cat', 'tox_desc'), 'grade', order = TRUE,
stratvar = 'phase', zeros = '-', n = c(5, 5), pct = TRUE)
stopifnot(
identical(
sort(unname(out[, grep('Total', colnames(out))[1]])),
sort(unname(out2[, grep('Total', colnames(out2))[1]]))
))
colnames(out2)[1:2] <- c(
'Description', sprintf('Total<br /><font size=1>n = %s</font>', sum(n))
)
cgroup <- c(
'', '',
sprintf('Phase I<br /><font size=1>n = %s</font>', n[1]),
sprintf('Phase II<br /><font size=1>n = %s</font>', n[2])
)
htmlTable::htmlTable(
out2, align = 'lc', cgroup = cgroup, n.cgroup = c(1, 1, 4, 4),
caption = 'Table 1: Toxicities<sup>†</sup> by category, phase,
grade.'
)
## collapse varname columns into single column with indents/extra rows
out3 <- tabler_by2(
tox, c('tox_cat', 'tox_desc'), 'grade', stratvar = 'phase', zeros = '-',
n = c(5, 5), pct = TRUE, pct.sign = FALSE,
collapse_varname = TRUE, collapse_format = c('<i>%s</i>', ' %s')
)
htmlTable::htmlTable(
out3, align = 'c', cgroup = cgroup[-1], n.cgroup = c(1, 1, 4, 4)[-1],
caption = 'Table 1: Toxicities<sup>†</sup> by category, phase,
grade.'
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.