gpt4 book ai didi

r - 在没有 "rownumber"(和书签)的情况下将 data.frame 从 R 转置到 Latex

转载 作者:行者123 更新时间:2023-12-04 10:58:39 25 4
gpt4 key购买 nike

我想转置(即向量作为列)R 中的 data.frame 并将其导出到没有行号但有列的 Latex。我想要这样的输出:

Tab

但是

df <- data.frame(qwertz=c("a","b","c","d","e","f"), asdfg=c("a","b"))

    qwertz asdfg
1 a a
2 b b
3 c a
4 d b
5 e a
6 f b

当我使用 xtable 和 booktabs 时

library(xtable)
print(xtable(t(df)), include.colnames=FALSE, booktabs=TRUE)

latex 输出是

Tab

print(xtable(t(df)), include.colnames=FALSE, booktabs=TRUE)
% latex table generated in R 3.0.2 by xtable 1.7-1 package
\begin{table}[ht]
\centering
\begin{tabular}{rllllll}
\toprule
\midrule
qwertz & a & b & c & d & e & f \\
asdfg & a & b & a & b & a & b \\
\bottomrule
\end{tabular}
\end{table}

问题:如何在两行之间直接插入\midrule

我也尝试使用 ENmisctoLatex()HmiscLatex() 阅读这些之后两个问题:

https://tex.stackexchange.com/q/25575/36408

https://tex.stackexchange.com/q/75793/36408

最佳答案

您可以使用add.to.row 参数

library(xtable)
df <- data.frame(qwertz=c("a","b","c","d","e","f"), asdfg=c("a","b"))
df <- t(df)
n <- nrow(df)

print(xtable(df), hline.after = NULL,
include.colnames = FALSE,
add.to.row = list(pos = list(0, 1, n),
command = c('\\toprule[1.5pt]\n', '\\midrule[1pt]\n', '\\bottomrule[1.5pt]\n')))
## % latex table generated in R 3.0.2 by xtable 1.7-1 package
## % Sat Jan 25 14:32:34 2014
## \begin{table}[ht]
## \centering
## \begin{tabular}{rllllll}
## \toprule[1.5pt]
## qwertz & a & b & c & d & e & f \\
## \midrule[1pt]
## asdfg & a & b & a & b & a & b \\
## \bottomrule[1.5pt]
## \end{tabular}
## \end{table}

要在 longtable LaTeX 环境中使用它,你可以这样做

print(xtable(df),
tabular.environment = "longtable",
floating = FALSE,
hline.after = NULL,
include.colnames = FALSE,
booktabs = TRUE,
add.to.row = list(pos = list(0, 1, n),
command = c('\\toprule[1.5pt]\n', '\\midrule[1pt]\n', '\\bottomrule[1.5pt]\n')))
## % latex table generated in R 3.0.2 by xtable 1.7-1 package
## % Mon Jan 27 13:00:01 2014
## \begin{longtable}{rllllll}
## \toprule[1.5pt]
## qwertz & a & b & c & d & e & f \\
## \midrule[1pt]
## asdfg & a & b & a & b & a & b \\
## \bottomrule[1.5pt]
## \end{longtable}

关于r - 在没有 "rownumber"(和书签)的情况下将 data.frame 从 R 转置到 Latex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21351031/

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