r - 如何使用 xtable 在表或数据框的列名中设置空格

标签 r sweave xtable

所以我想在数据框的列名称中的单词之间使用空格,并将数据框以 latex 格式打印为 pdf。但是,当我使用 xtable() 执行此操作时,列的名称会被连接。

例如,我希望第二列的标题看起来为“Categoria de Referencia”。相反,我得到的是:

enter image description here

有人知道如何解决这个问题吗?这是将数据帧转换为 latex 的 R block :

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

这是生成数据帧的代码:

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")

最佳答案

您最好的选择可能是使用sanitize.*选项,例如:

> 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}

来自 ?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

这与 sanitize.colnames.function 相同。

如果您想要彻底修改名称(这样您就无法像上面那样通过正则表达式实现自由),另一个选择是转换 dat到一个矩阵并设置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)

关于r - 如何使用 xtable 在表或数据框的列名中设置空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32121183/

相关文章:

r - 我如何知道 R 是在 64 位还是 32 位上运行?

r - Dplyr滚动天平

r - 如何在ggplot2图中摆脱空白?

r - 如何在 xtable 表中放置颜色间距?

r - Sweave 如何在 latex 表环境中编写 R 代码?

r - 当 .Rnw 有一个带有 "results"选项的 block 时,R 中的 Sweave 函数失败

r - Sweave 中的 xtable : specify toprule or midrule in a table + alignement

r - 向 R 中的表添加额外的列名和行名?

r - 汇总表(使用tables或xtable包),具有3级深度的计数

r - 我在if条件下缺少值错误