Question

I've got a CSV file that looks like this:

RTT,From,Req,Bytes,TTL
202,10.0.0.10,1,64,64
191,10.0.0.10,2,64,64
...

I am trying to produce a LaTeX summary() using library(xtable), like so:

library(xtable)
pings <- read.csv("pings.csv")
s <- summary(pings$RTT)
xtable(t(s))

This produces the output:

\begin{table}[ht]
\centering
\begin{tabular}{rrrrrrr}
  \hline
 & Min. & 1st Qu. & Median & Mean & 3rd Qu. & Max. \\
  \hline
1 & 40.70 & 42.70 & 43.40 & 44.90 & 44.10 & 202.00 \\
   \hline
\end{tabular}
\end{table}

This is almost what I want, except for the first column containing an empty value and 1.

Clearly, I'm missing some vital, basic knowledge about data types and conversions in R. The problem is of course that t(s) produces:

      Min. 1st Qu. Median  Mean 3rd Qu.  Max.
[1,]  40.7    42.7   43.4  44.9    44.1 202.0

wherein the [,1] should explain xtable's output.

Can anyone please point out to me what I'm doing wrong?

If I'm simply trying to run xtable on the summary, I get

> xtable(summary(pings$RTT))
Error in xtable.table(summary(pings$RTT)) : 
  xtable.table is not implemented for tables of > 2 dimensions
Was it helpful?

Solution 2

This is handled by print.xtable():

> library(xtable)
> print.xtable(xtable(t(summary(runif(10)))), include.rownames=FALSE)
% latex table generated in R 3.0.2 by xtable 1.7-3 package
% Sat Apr 26 20:03:32 2014
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrr}
  \hline
Min. & 1st Qu. & Median & Mean & 3rd Qu. & Max. \\ 
  \hline
0.03 & 0.18 & 0.48 & 0.41 & 0.61 & 0.74 \\ 
   \hline
\end{tabular}
\end{table}

Here include.rownames=FALSE disables outputting row names. See ?print.xtable for more details.

OTHER TIPS

xtable was fist developed for tables, in particular for 2 dimensional contingency tables, and then extended to various other output that might come in this way. It was never extended to objects of summaryDefault class which is what your s object is. When you coerce it to a matrix with t it becomes a 2 dimensional object and it then gets coerces to a dataframe with one rowname, "1" which gets printed by default. @gagolews gave you the way to get around it although I thought the help pages for xtable were a bit skimpy on details. It is helpful to look at the code. although most of it is hidden:

> methods(xtable)
 [1] xtable.anova*           xtable.aov*             xtable.aovlist*        
 [4] xtable.coxph*           xtable.data.frame*      xtable.glm*            
 [7] xtable.lm*              xtable.matrix*          xtable.prcomp*         
[10] xtable.summary.aov*     xtable.summary.aovlist* xtable.summary.glm*    
[13] xtable.summary.lm*      xtable.summary.prcomp*  xtable.table*          
[16] xtable.ts*              xtable.zoo*            

   Non-visible functions are asterisked
> getAnywhere(xtable.matrix)
A single object matching ‘xtable.matrix’ was found
It was found in the following places
  registered S3 method for xtable from namespace xtable
  namespace:xtable
with value

function (x, caption = NULL, label = NULL, align = NULL, digits = NULL, 
    display = NULL, ...) 
{
    return(xtable.data.frame(data.frame(x, check.names = FALSE), 
        caption = caption, label = label, align = align, digits = digits, 
        display = display))
}
<environment: namespace:xtable>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top