Create a table in R with header expanding on two columns using xtable or any package

此生再无相见时 提交于 2019-11-29 02:22:50

The tables package has a different approach to constructing tables and has a latex.tabular method to output the relevant latex.

This will work best if your data is in long form

library(tables)

# rbind with rownames as a column 
st <- rbind(
  data.frame(stT1, station = 'T1', what = factor(rownames(stT1), levels = rownames(stT1)), 
             row.names= NULL, check.names = FALSE), 
  data.frame(stT2,station = 'T2',what = factor(rownames(stT2), levels = rownames(stT2)), 
             row.names = NULL,check.names = FALSE)
     )


mytable <- tabular(Heading()*what ~ station*(`Observed-modeled` +`|observed-modeled|`)*Heading()*(identity),data=st)
mytable                                                                                
 ##         station                                                                
 ##         T1                                  T2                                 
 ##         Observed-modeled |observed-modeled| Observed-modeled |observed-modeled|
 ## Min.    -1.5360000       0.0001891          -2.3740          0.001259          
 ## 1st Qu.  0.0002512       0.1633000          -1.2280          0.674700          
 ## Median   0.3593000       0.5390000          -0.6202          1.101000          
 ## Mean     0.8554000       1.0020000          -0.2094          1.085000          
 ## 3rd Qu.  1.6470000       1.6470000           0.7418          1.413000          
 ## Max.     5.5370000       5.5370000           5.0530          5.053000

The is a great deal of fine control you have over the outcomes. These are described in the vignette.

In the example above you can see that Heading() will suppress (or change) the headings for a column while identity is the function which is used to summarize each combination.

And to show that it will create the appropriate tabular latex object (with multicolumns)

latex(mytable)


\begin{tabular}{lcccc}
\hline
 & \multicolumn{4}{c}{station} \\ 
 & \multicolumn{2}{c}{T1} & \multicolumn{2}{c}{T2} \\ 
  & Observed-modeled & |observed-modeled| & Observed-modeled & \multicolumn{1}{c}{|observed-modeled|} \\ 
\hline
Min.  & $-1.5360000$ & $0.0001891$ & $-2.3740$ & $0.001259$ \\
1st Qu.  & $\phantom{-}0.0002512$ & $0.1633000$ & $-1.2280$ & $0.674700$ \\
Median  & $\phantom{-}0.3593000$ & $0.5390000$ & $-0.6202$ & $1.101000$ \\
Mean  & $\phantom{-}0.8554000$ & $1.0020000$ & $-0.2094$ & $1.085000$ \\
3rd Qu.  & $\phantom{-}1.6470000$ & $1.6470000$ & $\phantom{-}0.7418$ & $1.413000$ \\
Max.  & $\phantom{-}5.5370000$ & $5.5370000$ & $\phantom{-}5.0530$ & $5.053000$ \\
\hline 
\end{tabular}

As noted above you can remove any column header using Heading()* the column in question

eg to remove "station" from the first row (as a header for all columns)

tabular(Heading()*what ~ Heading()*station*(`Observed-modeled` +`|observed-modeled|`)*Heading()*(identity),data=st)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!