R data.frame具有堆叠的指定标题,用于使用xtable输出乳胶 [英] R data.frame with stacked specified titles for latex output with xtable

查看:91
本文介绍了R data.frame具有堆叠的指定标题,用于使用xtable输出乳胶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

> w<-data.frame(c(0,0,1,1.3,2.1), c(0,0.6,0.9,1.6091,1.6299), c(258,141,206.4,125.8,140.5), c(162,162.7,162.4,162,162))
> colnames(w) <- c('Worst Cum', 'Best Cum', 'Worst Points', 'Best Points' )

错误(代码)

  Worst Cum Best Cum Worst Points Best Points
1       0.0   0.0000        258.0       162.0
2       0.0   0.6000        141.0       162.7
3       1.0   0.9000        206.4       162.4
4       1.3   1.6091        125.8       162.0
5       2.1   1.6299        140.5       162.0

目标:如何?

            CUM                    Points
        Worst Best          Worst       Best 
1       0.0   0.0000        258.0       162.0
2       0.0   0.6000        141.0       162.7
3       1.0   0.9000        206.4       162.4
4       1.3   1.6091        125.8       162.0
5       2.1   1.6299        140.5       162.0

------------- 示例(如果您知道上述解决方案,请进一步阅读) -------------

------------- Examples (needles to read further if you know the solution to the above) -------------

试验1:失败,出现许多data.frames

> a<-data.frame(c(0,0,1,1.3,2.1), c(0,0.6,0.9,1.6091,1.6299)) 
> b<-data.frame(c(258,141,206.4,125.8,140.5), c(162,162.7,162.4,162,162))
> c<-data.frame(cbind(a,b))
> colnames(c) <- c('Cum', 'Points')
> colnames(a) <- c('Worst', 'Best')
> colnames(b) <- c('Worst', 'Best')

> xtable(c)
% latex table generated in R 2.13.1 by xtable 1.6-0 package
% Thu Nov 24 03:43:34 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrr}
  \hline
 & Cum & Points & NA & NA \\ 
  \hline
1 & 0.00 & 0.00 & 258.00 & 162.00 \\ 
  2 & 0.00 & 0.60 & 141.00 & 162.70 \\ 
  3 & 1.00 & 0.90 & 206.40 & 162.40 \\ 
  4 & 1.30 & 1.61 & 125.80 & 162.00 \\ 
  5 & 2.10 & 1.63 & 140.50 & 162.00 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

> xtable(a)
% latex table generated in R 2.13.1 by xtable 1.6-0 package
% Thu Nov 24 03:45:06 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrr}
  \hline
 & Worst & Best \\ 
  \hline
1 & 0.00 & 0.00 \\ 
  2 & 0.00 & 0.60 \\ 
  3 & 1.00 & 0.90 \\ 
  4 & 1.30 & 1.61 \\ 
  5 & 2.10 & 1.63 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

这是错误的,因为它将内部标头替换为更高级别的标头nb"NA" vals.

It is wrong because it replaces the inner headers with higher-level header nb "NA" vals.

推荐答案

Hmisc包中的latex函数可以解决问题.这是代码(确保在序言中包含\usepackage{booktabs}以获得更好的表格式)

The latex function from Hmisc package will do the trick. Here is the code (make sure to include \usepackage{booktabs} in your preamble to get nicer table formatting)

library(Hmisc)
latex(w, file = "", booktabs = TRUE, title = "", cgroup = c('Cum', 'Points'), 
  colheads = rep(c('Worst', 'Best'), 2))

这篇关于R data.frame具有堆叠的指定标题,用于使用xtable输出乳胶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆