3

I want to use xtable to display a simple data frame containing two vectors with logical values:

ia = c(TRUE, TRUE, FALSE)
ib = c(FALSE, TRUE, TRUE)
i.df = data.frame(ia, ib)
xtable(i.df)

This produces:

\begin{table}[ht]
\begin{center}
\begin{tabular}{rll}
  \hline
 & ia & ib \\ 
  \hline
1 & c(TRUE, TRUE, FALSE) & c(FALSE, TRUE, TRUE) \\ 
  2 & c(FALSE, TRUE, TRUE) & c(TRUE, TRUE, FALSE) \\ 
  3 & c(TRUE, TRUE, FALSE) & c(FALSE, TRUE, TRUE) \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

However, I would have expected:

\begin{table}[ht]
\begin{center}
\begin{tabular}{rll}
  \hline
 & ia & ib \\ 
  \hline
1 & TRUE & FALSE \\ 
  2 & TRUE & TRUE \\ 
  3 & FALSE & FALSE \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

For some reason xtable prints the full logical vectors in each line, instead of printing only one element per line. Does this make sense to anybody or am I overlooking something blatantly obvious?

severin
  • 2,106
  • 1
  • 17
  • 25

1 Answers1

4

This feels like a bug to me. The relevant lines in xtable.data.frame are:

logicals <- unlist(lapply(x, is.logical))
x[, logicals] <- as.character(x[, logicals])

That isn't how I'd do that coercion to character. I'd probably do something more like:

x[,logicals] <- lapply(x[,logicals],as.character)

I would consider contacting the package author. In the mean time, you can try converting the logical columns to characters beforehand, yourself, as this will likely fix the problem in the meantime.

EDIT

Roman's comment below alerted me to the fact that my suggested fix won't work if there is only one logical column, since the dimensions are dropped by default. It should have been:

x[,logicals] <- lapply(x[,logicals,drop = FALSE],as.character)

I have contacted the package maintainer about this (2012-04-22).

joran
  • 169,992
  • 32
  • 429
  • 468
  • 1
    Thanks a lot! Converting to characters using `i.df$ia = as.character(i.df$ia)` works fine as a workaround. I also sent an email the package author. – severin Sep 29 '11 at 08:22