0

I,m new to R and LATEX(sorry for the spelling, but I don't know how to generate the classic version of latex here on OS). Anyway, here is my question.

I have a little bit long but narrow table, so I want to break them in two and make them look like enter image description here

the latex will automatically add .1 to the headers of the right half, like "Industry" in the left part but "Industry.1" in the right, what I want is "Industry" on both half, below is my data and code.

any helpful suggestion will be deeply appreciated.

\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}
Jia Gao
  • 1,172
  • 3
  • 13
  • 26
  • Hopefully my answer is what you are looking for, but if not, please provide a snippet of your data so I can replicate what you are doing. You can do this by pasting the output of `dput(INDUSTRY.DATA)` or, if its really big, `dput(head(INDUSTRY.DATA))`. Good luck. – Michael Davidson Sep 02 '16 at 15:44
  • Your solution works very well for me. I'm also wondering is there any way in xtable or Latex could generate table like this automatically? – Jia Gao Sep 03 '16 at 00:56

1 Answers1

0

Column names can repeat, just define them after creating the xtable object and before invoking the print command, like this:

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
  • 1,391
  • 1
  • 14
  • 31