Hi Louis, You pose an interesting question. Bellow is my take on a solution.
It is a function named "compare.all.column.pairs" which gets as input the original data (as a data.frame or a matrix, with all the columns you'd want to compare). And also the function you would like to use on each pair of columns. The function then outputs a list with the output of the function for every pair of column. Also, if you'd want the output to be in the form of a data.frame, there is the return_as_data.frame parameter you can set to TRUE and it will do so. However, in order for that to work, you must have the FUNCTION you enter, output something that can easily be turned into a data.frame. Beneath the function there is also two examples (one for t.test, and another for chisq.test) I hope this helps, Cheers, Tal compare.all.column.pairs <- function(DATA, FUNCTION, return_as_data.frame = F, ...) { # FUNCTION should always be of the form that can receive two parameters: function(column1, column2) number_of_columns <- dim(DATA)[2] all_collumns_combinations <- combn(seq_len(number_of_columns), 2) number_of_combinations <- dim(all_collumns_combinations)[2] FUNCTION_paits_output <- vector("list", number_of_combinations) list_names <- vector("character", number_of_combinations) if(is.null(colnames(DATA))) colnames(DATA) <- paste("Column",seq_len(number_of_columns), sep = "_") for(i in seq_len(number_of_combinations)) { column1 <- all_collumns_combinations[1,i] column2 <- all_collumns_combinations[2,i] FUNCTION_paits_output[[i]] <- FUNCTION(DATA[,column1], DATA[,column2] ,...) # give this list item a name list_names[i] <- paste("Pair number ", i,": ", paste(colnames(DATA)[c(column1, column2)], collapse = " VS ") , sep = "") } # set names for the items in the list attr(FUNCTION_paits_output, "names") <- list_names output <- FUNCTION_paits_output if(return_as_data.frame) { require(plyr) combo_dataframe <- t(all_collumns_combinations) colnames(combo_dataframe) <- c("Column1", "Column2") output <- ldply(FUNCTION_paits_output, function(x) {x}) output <- cbind(combo_dataframe, output) } return(output) } set.seed(341) norm_matrix <- matrix(rnorm(15*4), 15, 4) compare.all.column.pairs(norm_matrix, t.test, return_as_data.frame = F) # something with chi square set.seed(341) rounded_norm_matrix <- round(matrix(rnorm(15*4), 15, 4)) make.table.and.chi.square <- function(x,y) {chisq.test(table(x,y))$p.value} compare.all.column.pairs(rounded_norm_matrix, make.table.and.chi.square, return_as_data.frame = T) ----------------Contact Details:------------------------------------------------------- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Wed, Apr 13, 2011 at 10:32 PM, Louis Plough <lplo...@usc.edu> wrote: > lets<-c("ab","ac","bc","bd") > > epi<-data.frame(cbind("M1"= c(sample(lets,10, > replace=TRUE)),"M2"=c(sample(lets,10,replace=TRUE)), "M3"=c(sample(lets,10, > replace=TRUE)))) > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.