tabler_by: tabler_by

Description Usage Arguments Details Examples

Description

This function is helpful to make simple stratified tables, faster and easier to use than tabular.

Usage

1
tabler_by(dat, varname, byvar, n, order = FALSE, zeros, pct.col = FALSE)

Arguments

dat

a data frame; variables varname and byvar should be factors

varname

variable with subgroups to count

byvar

stratification variable

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 0 (0%) if not given

pct.col

logical; if TRUE, percents are separated into new columns

Details

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 percentages. 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, i.e., 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 to dat and will use the corresponding n to show percentages out of each respective subgroup.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
## generate data
set.seed(1)
f <- function(x, ...) sample(x, 100, replace = TRUE, ...)
tox <- data.frame(id = rep(1:10, 10), phase = 1:2,
                  tox_code = f(rawr::ctcae_v4$tox_code[1:25]),
                  tox_grade = f(1:3, prob = c(.6, .3, .1)),
                  stringsAsFactors = FALSE)

n <- table(tox[1:10, ]$phase)
tox <- cbind(tox,
  rawr::match_ctc(tox$tox_code)$matches[, c('tox_cat', 'tox_desc')])

tox <- within(tox, {
  phase <- factor(phase)
  tox_grade <- factor(tox_grade)
  tox_cat <- factor(tox_cat)
  tox_desc <- factor(tox_desc)
})

## get worst toxicities by casenum by grade
tox <- tox_worst(tox)$tox_worst

## summarize and format matrix for printing
out <- cbind(tabler_by(tox, 'tox_desc',
                       'phase', n = n, zeros = '-')[, 1, drop = FALSE],
             tabler_by(tox[tox$phase == '1', ], 'tox_desc',
                       'tox_grade', n = n[1], zeros = '-'),
             tabler_by(tox[tox$phase == '2', ], 'tox_desc',
                       'tox_grade', n = n[2], zeros = '-'))
out <- out[order(as.numeric(out[, 1]), decreasing = TRUE), ]

cgroup <- c(sprintf('Total<br /><font size=1>n = %s</font>', sum(n)),
            sprintf('Phase I<br /><font size=1>n = %s</font>', n[1]),
            sprintf('Phase II<br /><font size=1>n = %s</font>', n[2]))

library('htmlTable')
htmlTable(out, ctable = TRUE, cgroup = cgroup, n.cgroup = c(1, 4, 4),
    caption = 'Table 1: Toxicities<sup>&dagger;</sup> by phase and grade.',
    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>&dagger;</sup>Percentages represent ',
            'proportion of patients out of respective phase total.</font>'))
            

raredd/trainr documentation built on May 27, 2019, 2:03 a.m.