File: expand.table.R

package info (click to toggle)
r-cran-epitools 1%3A0.5-10-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 596 kB
  • sloc: makefile: 2
file content (20 lines) | stat: -rw-r--r-- 614 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
expand.table <-
function (x) 
{
    if (is.null(dimnames(x)) == TRUE) 
        stop("must have dimnames")
    if (any(names(dimnames(x)) == "")) 
        stop("must have names")
    tablevars <- expand.grid(rev(dimnames(x)))
    if (length(dim(x)) > 1) {
        ftablex <- ftable(x)
        counts <- as.vector(t(ftablex[, 1:ncol(ftablex)]))
    } else {
        counts <- as.vector(x)
    }
    expansion.index <- rep(1:nrow(tablevars), counts)
    newdat <- tablevars[expansion.index, , drop=FALSE]
    row.names(newdat) <- 1:nrow(newdat)
    revnames <- rev(names(newdat))
    newdat[, revnames, drop=FALSE]
}