Juliet Hannah
2008-Jan-29 16:56 UTC
[R] q about efficient cross-classification/creating tables
Dear R Users, I am posting a smaller portion of a question I posted last week. Code I have written takes weeks to run, and I am trying to make all the lines as efficient as possible. Here is one part of the code. I have some data that looks like [,1] [,2] [,3] [,4] [1,] 1 3 1 3 [2,] 3 3 2 3 [3,] 1 3 3 3 and a binary response. I am trying to obtain a vector of counts for all cross classifications of the response and each pair of columns from the data. Have I coded this in an efficient manner? library(combinat) # some example data myData <- matrix(sample(c(1:3),500,replace=TRUE),nrow=100,ncol=4) response <- c(rep(1,50),rep(0,50)) #initialize result matrix count.matrix <- matrix(-1,ncol=6,nrow=18); # indices of all pairs of columns all.pairs <- combn2(1:ncol(myData)) getCounts <- function(index) { counts <- as.vector(table(myData[,index[1]],myData[,index[2]],response)); } count.matrix <- apply(all.pairs, 1, getCounts) Thanks, Juliet [[alternative HTML version deleted]]