开发者

Create an xtable of a data.frame containing logical values

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开发者_JAVA百科 each line, instead of printing only one element per line. Does this make sense to anybody or am I overlooking something blatantly obvious?


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜