2

Suppose I have a data frame in the following form:

    N1  N2  N3  N4  N5  N6
     1   0   0   1   0   0
     0   1   0   1   0   1
     1   1   1   0   0   1
     0   0   0   1   1   0
     1   1   0   0   0   1

I would like to write a function that transform the above data frame to a contingency table like this:

            (N2=0,N3=0) (N2=0,N3=1) (N2=1,N3=0) (N2=1,N3=1)      
     N5=0       1            0           2           0
     N5=1       1            0           0           1

where I can specify what variables constitute the column and row. If possible, a function where I can substitute different data frame into it as well. Thanks!

mackbox
  • 199
  • 5

2 Answers2

4

Assuming df is your dataframe:

with(df, t(table(paste0(N2, N3), N5)))
N5  00 10 11
  0  1  2  1
  1  1  0  0
ytk
  • 2,787
  • 4
  • 27
  • 42
1

Perhaps not a perfect solution, but consider this function:

f <- function(df, select) {

    generate.levels <- function(...) {
        x <- do.call(expand.grid, rev(list(...)))
        if (ncol(x) > 1) x <- x[,ncol(x):1]
        for (i in 1:ncol(x)) x[,i] <- sprintf("%s=%s", names(x)[i], x[,i])
        x <- apply(x, 1, paste, collapse=",")
        x <- paste0("(", x, ")")
        x
    }

    x <- subset(df, select=select)
    l <- do.call(generate.levels, lapply(x, unique))
    for (i in 1:ncol(x)) x[,i] <- sprintf("%s=%s", names(x)[i], x[,i])
    x <- apply(x, 1, paste, collapse=",")
    x <- paste0("(", x, ")")
    factor(x, levels=l)
}

table(f(df, "N5"), f(df, c("N2", "N3")))

       (N2=0,N3=0) (N2=0,N3=1) (N2=1,N3=0) (N2=1,N3=1)
(N5=0)           1           0           2           1
(N5=1)           1           0           0           0
mrbrich
  • 853
  • 1
  • 8
  • 9