Search code examples
rsweavextable

How to set spaces in the column names of a table or data frame using xtable


So I want to use spaces among words in the names of the columns of a data frame and print the data frame to a pdf in latex format. However, when I use xtable() to do this, the names of the columns get joined.

For example, I'd want the title of the second column to look as "Categoria de Referencia". Instead, what I get is:

enter image description here

Does someone know how to sort this out? Here's the R chunk that translates the data frame into latex:

tab<-xtable(dat,caption="Efectos de las Variables sobre la Probabilidad de Conversión",digits=2)
print(tab,latex.environments = "center",include.rownames=FALSE)

Here's the code that generates the dataframe:

structure(list(Variable = structure(c(4L, 3L, 3L, 3L, 3L, 3L, 
2L, 1L), .Label = c("App", "Clicker", "País", "Uso"), class = "factor"), 
    Categoría.de.Referencia = structure(c(3L, 1L, 1L, 1L, 1L, 
    1L, 2L, 4L), .Label = c("Brasil", "Clicker", "No Acaba Unidad", 
    "No Usa App"), class = "factor"), Categoría.Considerada = structure(c(1L, 
    2L, 3L, 4L, 5L, 7L, 6L, 8L), .Label = c("Acaba la Unidad", 
    "España", "Francia", "ITalia", "Méjico", "No Cliker", "Otros Países", 
    "Usa App"), class = "factor"), Variación = structure(c(8L, 
    4L, 5L, 6L, 1L, 2L, 3L, 7L), .Label = c("-0.19%", "-0.46%", 
    "-2.32%", "0.31%", "0.46%", "0.7%", "1.15%", "2.08%"), class = "factor")), .Names = c("Variable", 
"Categoría.de.Referencia", "Categoría.Considerada", "Variación"
), row.names = c(NA, -8L), class = "data.frame")

Solution

  • Your best bet is probably to use the sanitize.* options, e.g.:

    > print.xtable(xtable(dat),include.rownames=F,
                   sanitize.colnames.function=function(x)gsub("\\."," ",x))
    
    % latex table generated in R 3.2.1 by xtable 1.7-4 package
    % Thu Aug 20 10:47:54 2015
    \begin{table}[ht]
    \centering
    \begin{tabular}{llll}
      \hline
    Variable & Categoría de Referencia & Categoría Considerada & Variación \\ 
      \hline
    Uso & No Acaba Unidad & Acaba la Unidad & 2.08\% \\ 
      País & Brasil & España & 0.31\% \\ 
      País & Brasil & Francia & 0.46\% \\ 
      País & Brasil & ITalia & 0.7\% \\ 
      País & Brasil & Méjico & -0.19\% \\ 
      País & Brasil & Otros Países & -0.46\% \\ 
      Clicker & Clicker & No Cliker & -2.32\% \\ 
      App & No Usa App & Usa App & 1.15\% \\ 
       \hline
    \end{tabular}
    \end{table}
    

    From ?print.xtable:

    sanitize.text.function All non-numeric enteries (except row and column names) are sanitised in an attempt to remove characters which have special meaning for the output format. If sanitize.text.function is not NULL (the default), it should be a function taking a character vector and returning one, and will be used for the sanitization instead of the default internal function

    This is the same for sanitize.colnames.function.

    Another option in case you'd like a total overhaul of the names (so that you can't regex your way to freedom as above) is to convert dat to a matrix and set the colnames:

    datmat<-as.matrix(dat)
    colnames(datmat)<-c("Variable","Categoría de Referencia",
                        "Categoría Considerada", "Variación")
    print.xtable(xtable(datmat),include.rownames=F)