3

我正在使用 R 和 LaTeX 以及用于表格的 xtable 包。我有点困惑为什么我指定的行名没有出现在我的表格中。这是一个例子:

\documentclass[a4paper, 11pt]{article}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\begin{document}
\SweaveOpts{concordance=TRUE}
<<results=tex, fig=FALSE, echo=FALSE>>=
library(xtable)
table.matrix <- matrix(numeric(0), ncol = 5, nrow = 12)
colnames(table.matrix) <- c("$m_t$", "$p_t$", "$R^b_t$", "$R^m_t$", "$y^r_t$")
rownames(table.matrix) <- c("$m_{t-1}$", " ", "$p_{t-1}$", " ", "$R^b_{t-1}$", " ", "$R^m_{t-1}$", " ", "$y^r_t$", " ", "$c$", " ")

tex.table <- xtable(table.matrix)
align(tex.table) <- "c||ccccc"
print(tex.table, include.rownames = TRUE, hline.after = c(-1, 0, seq(0, nrow(table.matrix), by = 2)), sanitize.text.function = function(x){x})
@
\end{document}

这对我来说似乎很简单。基本上我所拥有的是一个简单的 VAR 模型的估计,在行中我想要带有 (t-1) 下标的变量,并且每隔一行应该有 p 值(它们现在在名称向量中是空的)。有任何想法吗?

编辑:正如 nograpes 所指出的,一些行名重复的问题。有谁知道如何规避这个(不使用第一列作为名称列)?我只希望偶数行为空,或者某种指示它们是 p 值。

4

1 回答 1

6

尝试只运行 R 代码,你会得到一个警告,告诉你为什么行名没有出现:

library(xtable)
table.matrix <- matrix(numeric(0), ncol = 5, nrow = 12)
colnames(table.matrix) <- c("$m_t$", "$p_t$", "$R^b_t$", "$R^m_t$", "$y^r_t$")
rownames(table.matrix) <- c("$m_{t-1}$", " ", "$p_{t-1}$", " ", "$R^b_{t-1}$", " ", "$R^m_{t-1}$", " ", "$y^r_t$", " ", "$c$", " ")

xtable(table.matrix)
# Warning message:
# In data.row.names(row.names, rowsi, i) :
#  some row.names duplicated: 4,6,8,10,12 --> row.names NOT used

So you can't have duplicated rownames in R. You could solve this by making a name "column" instead of using rownames.

于 2013-02-20T19:28:42.370 回答