2

How do I get xtable (though I also have this issue with pander.table) to assign Greek letters to columns of a data frame within the print function without me needing to render the table and then manually type in the Latex for the Greek letters?

Here's the data for a reproducible example:

#data in 
chiSq <- 1600
df <- 850
p <- 0.95
CFI <- 0.95
TLI <- 0.95 
RMSEA <- 0.04
LOWRMSEA <- 0.03
HIGHRMSEA <- 0.04

I typically have some data frame that looks like this.

fit.stat <- data.frame(chiSq, df, p, CFI, TLI, RMSEA, LOWRMSEA, HIGHRMSEA)

Here are some specific issues that I regularly encounter when I go to make a table of data frame with xtable:

  • What I would like to do is to change the chiSq to the symbol for chi-square. In latex this $x^2$ will render the appropriate symbol.
  • I also need the p to be in italics.
  • Finally, the LOWRMSEA and HIGHRMSEA are upper and lower bounds, and I'd like them to remove the column names and include the data in the column for RMSEA, in the same way you would confidence intervals.

The only way I have found to do this is to print the table first with this command

library(xtable)
print(xtable(fit.stat, caption = "Model Fit Information for CFA"), 
caption.placement="top",
type = "latex")

Which produces this:

\begin{table}[ht]
\centering
\caption{Model Fit Information for CFA}
\begin{tabular}{rrrrrrrrr}
  \hline
 & chiSq & df & p & CFI & TLI & RMSEA & LOWRMSEA & HIGHRMSEA \\ 
  \hline
1 & 1600.00 & 850.00 & 0.95 & 0.95 & 0.95 & 0.04 & 0.03 & 0.04 \\ 
   \hline
\end{tabular}
\end{table}

However, I then need to manually edit the table to create this:

\begin{table}[ht]
\centering
\caption{Model Fit Information for CFA}
\begin{tabular}{rrrrrrrrrrrr}
  \hline
 & $x^2$ & {\it df} & {\it p} & CFI & TLI & RMSEA\\ 
  \hline
&1600.00 & 850.00 & 0.00 & 0.95 & 0.95 & 0.04 (0.03 - 0.04) \\ 
   \hline
\end{tabular}
\end{table}

I'd like to be able to do this dynamically without manually editing the tables so that I can include it as a code chunk in markdown doc. Thanks.

bfoste01
  • 337
  • 1
  • 2
  • 14
  • can you explain more better? I have no trouble using greek letters in tables – rawr Jul 04 '14 at 02:06
  • 1
    So is your question "should you be manually editing?" If so, that's off topic for SO. If you would like help writing code so that you don't have to do those edits, you should make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) illustrating the problem and clarify the desired output. – MrFlick Jul 04 '14 at 02:07

1 Answers1

0

As far as merging the RMSEA & LOWRMSEA & HIGHRMSEA values, that's probably best done as a paste in the data.frame. For example

fit.stat <- data.frame(chiSq, df, p, CFI, TLI, 
    RMSEA = paste0(RMSEA , " (",LOWRMSEA," - ", HIGHRMSEA,")"))

Then for the column names, you can override the sanitization features of print.xtable by specifying a custom sanitize function. As i'm sure you've already figured out, normally all "special" LaTeX characters are stripped out or escaped so as to not interfere with layout. But, here we can create a function that will do the replacement for us. First, we define a function

formatcolheads<-function(x) {
    sanitize<-get("sanitize", parent.frame())
    x<-sanitize(x)
    x<-gsub("chiSq","$x^2$",x)
    x<-gsub("df","{\\\\it df}",x)
    x<-gsub("p","{\\\\it p}",x)
    x
}

Note that I do a bit of work to grab the default sanitize function and run that over the columns just to make sure there aren't any problems. Then I go about replacing the values you want to change. Note that we have to double escape slashes for the "it stuff. But then we use the function like this

print(xtable(fit.stat, caption = "Model Fit Information for CFA"), 
caption.placement="top", sanitize.colnames.function = formatcolheads,
type = "latex")

and that produces

\begin{table}[ht]
\centering
\caption{Model Fit Information for CFA} 
\begin{tabular}{rrrrrrl}
  \hline
 & $x^2$ & {\it df} & {\it p} & CFI & TLI & RMSEA \\ 
  \hline
1 & 1600.00 & 850.00 & 0.95 & 0.95 & 0.95 & 0.04 (0.03 - 0.04) \\ 
   \hline
\end{tabular}
\end{table}

which appears to be the output you desired.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • +1. @MrFlick: I'd like to up vote, but I need to wait until I'm around here long enough to get that permission enabled. This is a great solution, that should work with pander.table too. You improved my reproducible workflow quite a bit with the `sanitize` function. – bfoste01 Jul 04 '14 at 15:10
  • Note, in the solution you posted `x<-gsub("chiSq","$x^2",x)` needs to be `x<-gsub("chiSq","$x^2$",x)`. It's such a small edit SO won't let me fix it for you. Thanks again. – bfoste01 Jul 05 '14 at 13:42