How to make a latex table for 3d-array in R

1.1k views Asked by At

I have a 3d-array "arr" in R:

> v1 <- c("100", "75", "31", "41", "1000", "69")
> v2 <- c("10m", "6h", "5d", "11m", "6h", "5d")
> arr <- array(c(v1, v2), dim=c(3, 2, 2), dimnames=list(c("d1", "d2", "d3"), c("m1", "m2"), c("v1", "v2")))
> arr
, , v1

   m1    m2    
d1 "100" "41"  
d2 "75"  "1000"
d3 "31"  "69"  

, , v2

   m1    m2   
d1 "10m" "11m"
d2 "6h"  "6h" 
d3 "5d"  "5d" 

which I want to create a Latex table for it that will looks like:

          m1            m2
------------------------------
        v1  v2        v1  v2
------------------------------
d1     100 10m        41 11m
d2      75  6h      1000  6h
d3      31  5d        69  5d

How can I use R to make a table like the above into latex? I know the R package "xtable" can do similar things that I need. However, I don't know if it can make the exact table above for me.

PS: I also wonder if there are other ways to present a 3d-array in a table besides that presented above.

1

There are 1 answers

0
Thomas On
> library(xtable)
> xtable(cbind(arr[,'m1',], arr[,'m2',]), align='lrrrr')
% latex table generated in R 3.0.2 by xtable 1.7-1 package
% Fri Dec 13 07:48:13 2013
\begin{table}[ht]
\centering
\begin{tabular}{lrrrr}
  \hline
 & v1 & v2 & v1 & v2 \\ 
  \hline
d1 & 100 & 10m & 41 & 11m \\ 
  d2 & 75 & 6h & 1000 & 6h \\ 
  d3 & 31 & 5d & 69 & 5d \\ 
   \hline
\end{tabular}
\end{table}