Simple crosstable with row- and multicolumn columnnames from R to latex -


i trying produce simple crosstable in r , have exported latex using knitr in rstudio.

i want table publishable table, row header, column header, , subheaders each category of variable in column. since table have identical categories rows , columns, wish replace column level headers numbers. see example below:

                                      profession mother  professsionfather                1.          2.            3.  1. bla                      frequency   frequency    frequency  2. blahabblab  3. blahblahblah 

i getting close 'xtable' (i can't row , column headers print, , not multicolumn header), , 'tables' package (i can't replace column categories numbers).

minimal example:

work1 <- paste("longstring", 1:10, sep="") work2 <- paste("longstring", 1:10, sep="") t <- table(work1, work2) # making table t # table repated row/column names colnames(t) <- paste(1:10, ".", sep="") # replacing column names numeric values  xtable(t) # headers omitted both rows , columns  work <- data.frame(cbind(work1, work2)) # prepare use of tabular tabular((fathersprofession=work1) ~ (mothersprofession=work2), data=work) # have headers, no way change column categories "longstring"x numeric.  

you need assign output of tabular function named object:

 tb <- tabular((fathersprofession=work1) ~ (mothersprofession=work2), data=work)  str(tb) 

it should obvious data in list , column-names in attribute begins:

 - attr(*, "collabels")= chr [1:2, 1:10] "mothersprofession" "longstring1" na "longstring10" ... 

so

 attr(tb,  "collabels") <-         gsub("longstring", "" , attr(tb,  "collabels") ) 

this output screen, output latex device different.

> tb                     mothersprofession                     fathersprofession 1                 10 2 3 4 5 6 7 8 9  longstring1       1                 0  0 0 0 0 0 0 0 0  longstring10      0                 1  0 0 0 0 0 0 0 0  longstring2       0                 0  1 0 0 0 0 0 0 0  longstring3       0                 0  0 1 0 0 0 0 0 0  longstring4       0                 0  0 0 1 0 0 0 0 0  longstring5       0                 0  0 0 0 1 0 0 0 0  longstring6       0                 0  0 0 0 0 1 0 0 0  longstring7       0                 0  0 0 0 0 0 1 0 0  longstring8       0                 0  0 0 0 0 0 0 1 0  longstring9       0                 0  0 0 0 0 0 0 0 1 

enter image description here


Comments