ctab2: ctab2 Cross Tabulation (data frame outputs)

Description Usage Arguments Value See Also Examples

Description

The function builds contingency tables: counts and row proportions. ctab2 is an alternative to ctab. The difference is that outputs of ctab2 are data frames, while outputs of ctab are tables. Both ctab2 and ctab have the same syntax.

Three types of data are managed by ctab2:

For grouped data, ctab2 uses splitbin.

Usage

1
2
  ctab2(formula, data, weights = NULL, digits = 2)
  

Arguments

formula

A 2-sided formula describing the contincency table. The left-hand side represents the column-variable. See examples for syntax.

data

A data frame where formula is evaluated.

weights

Variable representing the weight for grouped data with weights.

digits

A scalar indicating the number of decimal digits left when rounding the result of the statictics.

Value

A list with components CALL, formula, f, tab, tab.p and digits. Component tab is the basic contingency table.

See Also

table, xtabs, ftable

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#=== Example of the 3 types of data managed by ctab2

    # Split data

tmp <- data.frame(
    treatment = c("B", "A", "C", "C", "B", "B", "C", "A", "B", "B", NA, "C"),
    surv = c("YES", "NO", "YES", "YES", "NO", "NO", "NO", "YES", "YES", "NO", "NO", NA)
    )
#tmp$treatment <- factor(tmp$treatment, levels = c("C", "A", "B"))
tmp

ctab2(formula = surv ~ 1, data = tmp)
ctab2(formula = surv ~ treatment, data = tmp, digits = 1)

    # Grouped data with weights

tmp <- data.frame(
    treatment = c("A", "B", "C", "A", "B", "C", NA, "C"),
    surv = c("NO", "NO", "NO", "YES", "YES", "YES", "NO", NA),
    n = c(1, 3, 1, 1, 2, 2, 1, 1)
    )
tmp

ctab2(formula = surv ~ 1, data = tmp, weights = n)
ctab2(formula = surv ~ treatment, data = tmp, weights = n)

    # Grouped data of the form "cbind(success, failure)"

tmp <- data.frame(
    treatment = c("A", "B", "C", NA),
    surv = c(1, 2, 2, 0),
    death = c(1, 3, 1, 1)
    )
tmp

ctab2(formula = cbind(surv, death) ~ 1, data = tmp)
ctab2(formula = cbind(surv, death) ~ treatment, data = tmp)

#=== Examples of tables

n <- 80
tmp <- data.frame(
    y1 = sample(1:2, n, replace = TRUE),
    y2 = sample(1:2, n, replace = TRUE),
    y3 = sample(1:3, n, replace = TRUE),
    y4 = sample(1:4, n, replace = TRUE)
    )
head(tmp)
ctab2(formula = y3 ~ 1, data = tmp)

res <- ctab2(formula = y3 ~ y1, data = tmp)
res$tab
res$tab.p

ctab2(formula = y3 ~ y1 + y2, data = tmp)
ctab2(formula = y3 ~ y1 + y2 + y4, data = tmp)

    

tdisplay documentation built on May 2, 2019, 4:46 p.m.