gpt4 book ai didi

r - 自定义 xtable

转载 作者:行者123 更新时间:2023-12-03 23:48:13 25 4
gpt4 key购买 nike

我要定制xtable用于导出到 LaTeX。我知道有些问题是关于 xtable在这里,但我找不到我要找的具体东西。

以下是我的表的外观示例:

my.table <- data.frame(Specifiers=c("","Spec1", "Spec2", "Spec3"),
Values1 = c("N=10", 1.03, 1.71, 2.25),
Values2 = c("N=20", 1.32, 1.79, 2.43))
colnames(my.table)[1] <- ""

这创造了:
         Values1 Values2
1 N=10 N=20
2 Spec1 1.03 1.32
3 Spec2 1.71 1.79
4 Spec3 2.25 2.43

事实上,这个表是从一个 .csv 文件导入的 data.framemy.table <- read.delim("filename.csv", sep=",", header=TRUE)
现在我用 xtable 创建一个 LaTeX 表:
latex.tab <- xtable(my.table, caption=c("Stats"))
print(latex.tab, file="Summarystats.tex",
floating.environment='sidewaystable',
include.rownames=FALSE,
booktabs=TRUE,
latex.environment=NULL)

这是生成的 LaTeX 代码:
\begin{sidewaystable}[ht]
\begin{tabular}{lllllll}
\toprule
& Values1 & Values2 \\
\midrule
N=10 & N=20 \\
Spec1 & 1.03 & 1.32 \\
Spec2 & 1.71 & 1.79 \\
Spec3 & 2.25 & 2.43 \\

\bottomrule
\end{tabular}
\end{sidewaystable}

好的,现在这就是我想要改变的:

1) 插入 \midrule在第二行之后而不是在第一行之后。
2) 通过插入 \rowcolors{2}{gray!25}{white} 来改变该表各行的颜色内 sidewaystable (或正常 table )环境。
3) 将列名称旋转 45°
4) 插入 \centering而不是 center -environment 在我想将表格居中的情况下。

关于如何实现这一目标的任何想法?

最佳答案

你需要一些预处理,额外的参数传递给 print.xtable以及一些后期处理:

my.table <- data.frame(Specifiers=c("","Spec1", "Spec2", "Spec3"),
Values1 = c("N=10", 1.03, 1.71, 2.25),
Values2 = c("N=20", 1.32, 1.79, 2.43))
colnames(my.table)[1] <- ""

# Pre-processing: rotates column names by 45 degrees
head = apply(as.array(names(my.table)), 1, function(x) paste("\\rotatebox{45}{", x, "}"))
head = paste(head, c(rep("&", length(head)-1), "\\\\\n"), collapse="")

latex.tab <- xtable(my.table, caption=c("Stats"))
ltable = print(latex.tab, file="", # File is empty, post-processing needed
floating.environment='sidewaystable',
include.rownames=FALSE,
include.colnames=FALSE, # No colnames
booktabs=TRUE,
latex.environment="center", # Or NULL
# Adds some extra-text after the rows specified in pos.
# Adds new \midrule and comments old one.
# Adds pre-processed names of columns
add.to.row=list(pos=as.list(c(0, 0, 1)), command=as.vector(c(head, "%", "\\midrule\n"))))

# Post-processing: replaces \begin{center} with \centering
ltable = sub("\\begin{center}\n", "\\centering\n", ltable, fixed=TRUE)
ltable = sub("\\end{center}\n", "\n", ltable, fixed=TRUE)

# Post-processing: adds alternating colours
ltable = sub("\\begin{tabular}",
"\\rowcolors{2}{gray!25}{white}\n\\begin{tabular}",
ltable, fixed=TRUE)

# Writes output to the file
cat(ltable, file="Summarystats.tex")

如果您需要其他选项卡环境而不是 tabular你可以
1) 添加新变量:
TABULAR = "tabular"

2) 将它的值传递给 print.xtable像这样:
...
tabular.environment=TABULAR,
...

3)改变你的后处理交替颜色:
ltable = sub(sprintf("\\begin{%s}", TABULAR),
sprintf("\\rowcolors{2}{gray!25}{white}\n\\begin{%s}", TABULAR),
ltable, fixed=TRUE)

结果:

enter image description here

关于r - 自定义 xtable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11777586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com