3

I am using R and LaTeX and the xtable package for tables. I am a little puzzled why my specified rownames aren't showing up in my tables. Here's an example:

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

It seems pretty straightforward to me. Essentially what I have is an estimation of a simple VAR model and in the rows I want the variables with a (t-1) subscript, and every second row should have the p-values (they're empty right now in the name vector). Any ideas?

EDIT: As pointed out by nograpes, the issue that some rownames are duplicates. Does anyone know how to circumvent this (without using the first column as a name column)? I just want the evenly numbered rows to be empty, alternatively some sort of indication they are p-values.

hejseb
  • 2,064
  • 3
  • 18
  • 28

1 Answers1

6

Try running just the R code, and you'll get a warning that tells you why the row names aren't showing up:

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.

nograpes
  • 18,623
  • 1
  • 44
  • 67
  • Well spotted. I tried using rownames(table.matrix) <- c("$m_{t-1}$", "test", "$p_{t-1}$", "test2", "$R^b_{t-1}$", "test3", "$R^m_{t-1}$", "test4", "$y^r_t$", "test5", "$c$", "test6"), which worked. So no duplicate row names, it seems. Can I force it to ignore that somehow? – hejseb Feb 20 '13 at 20:12
  • No, I doubt it. The row names have to be unique because they can be used for indexing the data frame (or matrix). – nograpes Feb 20 '13 at 20:31