【问题标题】:xtable same headers for a long narrow table break in twoxtable 长窄表的相同标题分成两部分
【发布时间】:2016-09-02 15:15:46
【问题描述】:

我是 R 和 LATEX 的新手(对不起拼写,但我不知道如何在 OS 上生成经典版本的 Latex)。无论如何,这是我的问题。

我的桌子有点长但很窄,所以我想把它们分成两半,让它们看起来像 enter image description here

乳胶会自动将 .1 添加到右半部分的标题中,例如左侧的“Industry”,而右侧的“Industry.1”,我想要的是两半的“Industry”,下面是我的数据和代码。

任何有用的建议将不胜感激。

\documentclass[11]{article}
\title{Privatization Report}
\author{JiaGao, PhD}
\usepackage{float}
\usepackage{array}
\usepackage[top = 0.5in, bottom = 0.5in, left = 1in, right = 1in]{geometry}
\begin{document}
\maketitle

INDUSTRY.DATA<-read.dta13("industrycode.dta")
DECRIB.DATA<-read.dta13("describe_V1.dta")
indu.count<-data.frame(table(DECRIB.DATA$industrycode2))
indu.count<-rename(indu.count,c(Var1 = "industrycode2"))
INDUSTRY.DATA<-merge(INDUSTRY.DATA,indu.count,by.x="industrycode2",
                     by.y = "industrycode2",all.x=TRUE)
na.list<-which(is.na(INDUSTRY.DATA$Freq == "NA"))
INDUSTRY.DATA$Freq<-replace(INDUSTRY.DATA$Freq, na.list,0)
INDUSTRY.DATA$Perc<-INDUSTRY.DATA$Freq/sum(INDUSTRY.DATA$Freq)
rm(na.list,indu.count)

<<echo = FALSE, results = 'asis'>>=
industryTable<-data.frame("Industry" = INDUSTRY.DATA[1:15,]$industryname,
                      "Freq" = INDUSTRY.DATA[1:15,]$Freq,
                      "Perc" = INDUSTRY.DATA[1:15,]$Perc,
                      "Industry" = INDUSTRY.DATA[16:30,]$industryname,
                      "Freq" = INDUSTRY.DATA[16:30,]$Freq,
                      "Perc" = INDUSTRY.DATA[16:30,]$Perc)

print(xtable(industryTable,caption = "Distribution of Privatization Across Manufacturing", label = "table:industry",align = c("c","p{4.5cm}","c","c","|p{4.5cm}","c","c"),digits = c(0,0,0,2,0,0,2)),caption.placement="top", include.rownames = FALSE)
@
\end{document}

【问题讨论】:

  • 希望我的答案是您正在寻找的,但如果不是,请提供您的数据的 sn-p,以便我可以复制您正在做的事情。您可以通过粘贴dput(INDUSTRY.DATA) 的输出来做到这一点,或者,如果它真的很大,可以粘贴dput(head(INDUSTRY.DATA))。祝你好运。
  • 您的解决方案非常适合我。我也想知道 xtable 或 Latex 中是否有任何方法可以自动生成这样的表格?

标签: r latex xtable


【解决方案1】:

列名可以重复,只需在创建xtable 对象之后和调用print 命令之前定义它们,如下所示:

tab<-xtable(data.frame(LETTERS[1:13], LETTERS[14:26]))

tab
#\begin{table}[ht]
#\centering
#\begin{tabular}{rll}
#  \hline
# & LETTERS.1.13. & LETTERS.14.26. \\ 
#  \hline
#  1 & A & N \\ 
#  2 & B & O \\ 
#  3 & C & P \\ 
#  4 & D & Q \\ 
#  5 & E & R \\ 
#  6 & F & S \\ 
#  7 & G & T \\ 
#  8 & H & U \\ 
#  9 & I & V \\ 
#  10 & J & W \\ 
#  11 & K & X \\ 
#  12 & L & Y \\ 
#  13 & M & Z \\ 
#   \hline
#\end{tabular}
#\end{table}


names(tab) <- c("le","le")

tab
# \begin{table}[ht]
# \centering
# \begin{tabular}{rll}
# \hline
# & le & le \\ 
# \hline
# 1 & A & N \\ 
# 2 & B & O \\ 
# 3 & C & P \\ 
# 4 & D & Q \\ 
# 5 & E & R \\ 
# 6 & F & S \\ 
# 7 & G & T \\ 
# 8 & H & U \\ 
# 9 & I & V \\ 
# 10 & J & W \\ 
# 11 & K & X \\ 
# 12 & L & Y \\ 
# 13 & M & Z \\ 
# \hline
# \end{tabular}
# \end{table}

【讨论】:

  • 这对我来说非常有用,非常感谢@Michael Davidson,
猜你喜欢
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
  • 1970-01-01
  • 1970-01-01
  • 2011-07-19
相关资源
最近更新 更多