[R] ftable as latex (with Hmisc?)

Dieter Menne dieter.menne at menne-biomed.de
Thu Nov 29 18:23:51 CET 2007


Dieter Menne wrote:
 
> I tried to create an ftable lookalike of the following data set in 
> LaTeX/Sweave

> with summary(formula,..), but I could not get it to work for count tables; 
> numeric tables work fine. 
> summary(formula,...,fun="table") does not give the full cross breakdown.


Frank E Harrell Jr <f.harrell <at> vanderbilt.edu> writes:

> See if fun=table (no quotes) or method='cross' work for you.
> Frank

The ftable-type-lookalike looks more extensible to me, as it works for 
any reasonable number of columns/rows. Here my quick and dirty version 
for Sweave (no file output, fixed, to ctable) if someone else needs it.

Dieter

#------------------------- 
latex.ftable = function(object,title="",caption,label,pos="htbp",...){
  # use default formatting of ftable as a starter
  ft = format(object,quote=FALSE) 
  cv = attr(object,"col.vars")
  rv = attr(object,"row.vars")
  nr = nrow(ft)
  nc = ncol(ft)
  ncolvars = length(cv)
  nrowvars = length(rv)
  
  ft[ncolvars,1:nrowvars] = ft[ncolvars+1,1:nrowvars]
 
  align1 = paste(rep("l",ncolvars),collapse="")
  align2 = paste(rep("r",nc-ncolvars),collapse="")
  cat("\\ctable[ caption={",caption,"}, label=",label,",pos=",pos,
    ", botcap]{",
    align1,align2,"}{} \n{\\FL\n", sep="")
  for (i in 1:ncolvars){
    head = paste("\\multicolumn{1}{c}{",ft[i,],"}",collapse="&\n",sep="")  
    if (i == ncolvars)
      cat(head, "\n\\ML\n") else
    cat(head, "\n\\NN\n")
      
  }
  for (i in (ncolvars+2):nr) { 
    cat(paste(ft[i,],collapse="&"))
    if (i != nr ) {
      if (substr(ft[i,1],1,1) ==' ' & substr(ft[i+1,1],1,1) !=' '){
        cat("\\ML\n")
      } else 
      cat("\\NN\n")
    }
  }
  cat("\n\\LL\n}\n")
}



More information about the R-help mailing list