Reputation: 11341
Say I have the following data frame:
df <- data.frame(sector = c(rep('a', 3), rep('b', 4)), id = 1:7)
I would like to create a summarized table reporting a list of IDs on each sector, so I do this:
table <- xtable(aggregate(id ~ sector, df, paste))
However, when I try to use xtable()
to transform the object above into LaTeX, I get the following error:
> xtable(table)
Error in is.finite(x) : default method not implemented for type 'list'
Which is caused by the second column, since class(table$id)
confirms it is a list.
It should be noted that xtable()
handles data frames as input well. Changing "paste" to something more common like "sum" or "length" doesn't yield errors.
What can I do to table
(or to xtable
) so it creates the table I want? The desired raw output should look something like this:
\begin{table}[ht]
\centering
\begin{tabular}{rlr}
\hline
& sector & id \\
\hline
1 & a & 1, 2, 3 \\
2 & b & 4, 5, 6, 7\\
\hline
\end{tabular}
\end{table}
Upvotes: 2
Views: 603
Reputation: 263301
Adding a 'collapse' argument to paste
will succeed:
> xtable(aggregate(id ~ sector, df, paste, collapse=","))
% latex table generated in R 3.0.1 by xtable 1.7-1 package
% Tue Aug 6 11:46:03 2013
\begin{table}[ht]
\centering
\begin{tabular}{rll}
\hline
& sector & id \\
\hline
1 & a & 1,2,3 \\
2 & b & 4,5,6,7 \\
\hline
\end{tabular}
\end{table}
Upvotes: 6