possible to create latex multicolumns in xtable? possible to create latex multicolumns in xtable? r r

possible to create latex multicolumns in xtable?


I think the add.to.row option in xtable achieves this perfectly.

Example code here:

require(xtable)age <- sample(c('30-50', '50-70', '70+'), 200, replace=T)sex <- sample(c('Male', 'Female'), 200, replace=T)val <- table(age, sex)val <- rbind(val, formatC(prop.table(val)*100, format='f', digits=1))val <- structure(val, dim=c(3, 4))val <- rbind(c('n', '%'), val)rownames(val) <- c('', sort(unique(age)))val <- xtable(val)addtorow <- list()addtorow$pos <- list(0)addtorow$command <- paste0(paste0('& \\multicolumn{2}{c}{', sort(unique(sex)), '}', collapse=''), '\\\\')print(val, add.to.row=addtorow, include.colnames=F)


Assuming the form of the table is the same across runs (i.e., only the numbers are changing), my suggestion would be to use the only.contents argument to print.xtable and code the multi-column headers in by hand. To the best of my knowledge xtable is not capable of doing multi-column cells itself.