this is a replacement for table that allows empty cells because the margins have been specified. The input vectors a and b must be the same length, this function does no argument checking.

cross_tab_margins(a, b, a_margin, b_margin)

Arguments

a

one set of observations

b

another set of observations

a_margin

number of distinct values of a (rows)

b_margin

number of distinct values of b (cols)

Examples

a <- 1:5
b <- c(1,2,3,1,2)
cross_tab_margins(a,b,5,3)
#>      [,1] [,2] [,3]
#> [1,]    1    0    0
#> [2,]    0    1    0
#> [3,]    0    0    1
#> [4,]    1    0    0
#> [5,]    0    1    0
table(a,b)
#>    b
#> a   1 2 3
#>   1 1 0 0
#>   2 0 1 0
#>   3 0 0 1
#>   4 1 0 0
#>   5 0 1 0