gpt4 book ai didi

r - 使用 xtable 或任何包在 R 中创建一个表,标题在两列上扩展

转载 作者:行者123 更新时间:2023-12-04 11:42:24 25 4
gpt4 key购买 nike

我想组合来自两个数据帧的表,并希望将该表导出到 latex 。我要创建的表如下所示。请注意,下表是使用 excel 创建的。

enter image description here

从 R 中的 xtable,我能够导出两个单独站的表。得到以下两个站 T1 和 T2。如何组合两个站的输出以获得上述所需的输出?

T1站:

> stT1
Observed-modeled |observed-modeled|
Min. -1.5360000 0.0001891
1st Qu. 0.0002512 0.1633000
Median 0.3593000 0.5390000
Mean 0.8554000 1.0020000
3rd Qu. 1.6470000 1.6470000
Max. 5.5370000 5.5370000

> dput(stT1)
structure(list(`Observed-modeled` = c(-1.536, 0.0002512, 0.3593,
0.8554, 1.647, 5.537), `|observed-modeled|` = c(0.0001891, 0.1633,
0.539, 1.002, 1.647, 5.537)), .Names = c("Observed-modeled",
"|observed-modeled|"), row.names = c("Min.", "1st Qu.", "Median",
"Mean", "3rd Qu.", "Max."), class = "data.frame")

T2站:
> stT2
Observed-modeled |observed-modeled|
Min. -2.3740 0.001259
1st Qu. -1.2280 0.674700
Median -0.6202 1.101000
Mean -0.2094 1.085000
3rd Qu. 0.7418 1.413000
Max. 5.0530 5.053000

> dput(stT2)
structure(list(`Observed-modeled` = c(-2.374, -1.228, -0.6202,
-0.2094, 0.7418, 5.053), `|observed-modeled|` = c(0.001259, 0.6747,
1.101, 1.085, 1.413, 5.053)), .Names = c("Observed-modeled",
"|observed-modeled|"), row.names = c("Min.", "1st Qu.", "Median",
"Mean", "3rd Qu.", "Max."), class = "data.frame")

我将两个站合并如下:
newstT <- cbind(stT1,stT2)

> newstT
Observed-modeled |observed-modeled| Observed-modeled |observed-modeled|
Min. -1.5360000 0.0001891 -2.3740 0.001259
1st Qu. 0.0002512 0.1633000 -1.2280 0.674700
Median 0.3593000 0.5390000 -0.6202 1.101000
Mean 0.8554000 1.0020000 -0.2094 1.085000
3rd Qu. 1.6470000 1.6470000 0.7418 1.413000
Max. 5.5370000 5.5370000 5.0530 5.053000

我无法在顶部添加站 T1 和站 T2,如所需图所示。

最佳答案

tables包有一种不同的方法来构建表,并且有一个 latex.tabular输出相关胶乳的方法。

如果您的数据是长格式,这将最有效

library(tables)

# rbind with rownames as a column
st <- rbind(
data.frame(stT1, station = 'T1', what = factor(rownames(stT1), levels = rownames(stT1)),
row.names= NULL, check.names = FALSE),
data.frame(stT2,station = 'T2',what = factor(rownames(stT2), levels = rownames(stT2)),
row.names = NULL,check.names = FALSE)
)


mytable <- tabular(Heading()*what ~ station*(`Observed-modeled` +`|observed-modeled|`)*Heading()*(identity),data=st)
mytable
## station
## T1 T2
## Observed-modeled |observed-modeled| Observed-modeled |observed-modeled|
## Min. -1.5360000 0.0001891 -2.3740 0.001259
## 1st Qu. 0.0002512 0.1633000 -1.2280 0.674700
## Median 0.3593000 0.5390000 -0.6202 1.101000
## Mean 0.8554000 1.0020000 -0.2094 1.085000
## 3rd Qu. 1.6470000 1.6470000 0.7418 1.413000
## Max. 5.5370000 5.5370000 5.0530 5.053000

这是您对结果的大量精细控制。这些在 vignette 中有描述.

在上面的例子中你可以看到 Heading()将抑制(或更改)列的标题,而 identity是用于总结每个组合的函数。

并表明它将创建适当的表格 latex 对象(具有多列)
latex(mytable)


\begin{tabular}{lcccc}
\hline
& \multicolumn{4}{c}{station} \\
& \multicolumn{2}{c}{T1} & \multicolumn{2}{c}{T2} \\
& Observed-modeled & |observed-modeled| & Observed-modeled & \multicolumn{1}{c}{|observed-modeled|} \\
\hline
Min. & $-1.5360000$ & $0.0001891$ & $-2.3740$ & $0.001259$ \\
1st Qu. & $\phantom{-}0.0002512$ & $0.1633000$ & $-1.2280$ & $0.674700$ \\
Median & $\phantom{-}0.3593000$ & $0.5390000$ & $-0.6202$ & $1.101000$ \\
Mean & $\phantom{-}0.8554000$ & $1.0020000$ & $-0.2094$ & $1.085000$ \\
3rd Qu. & $\phantom{-}1.6470000$ & $1.6470000$ & $\phantom{-}0.7418$ & $1.413000$ \\
Max. & $\phantom{-}5.5370000$ & $5.5370000$ & $\phantom{-}5.0530$ & $5.053000$ \\
\hline
\end{tabular}

如上所述,您可以使用 Heading()* 删除任何列标题。有问题的列

例如从第一行删除“站”(作为所有列的标题)
tabular(Heading()*what ~ Heading()*station*(`Observed-modeled` +`|observed-modeled|`)*Heading()*(identity),data=st)

关于r - 使用 xtable 或任何包在 R 中创建一个表,标题在两列上扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17560683/

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