4

I've seen similar questions on SO-most of them have not been answered. Those that were answered usually suggest not using xtable since it's not flexible to customization. I've attached two pictures: 1) my generated table from xtable 2) my ideal table. I'd like to add horizontal line to my 1.png dividing it into 3 sections and call sections as Schools, Grades, Students.

1) Does anybody see any possible solution with xtable?

2) Is there any way to read the output of xtable as a list of strings and then modify it by adding some latex code to get the desired table? Have you tried it?

3) Is there any other package that can be used instead of xtable? enter image description here enter image description here

Here are some simulated numbers to work with (let's suppose it's the code for 1.png):

library(xtable)

desc.matrix <- matrix(rep(NA,60),ncol = 4)
colnames(desc.matrix) <- c("Grade 3","Grade 4","Grade 5","Total")
rownames(desc.matrix) <- c("TRT-#Schools","TRT(Enroll>=85%)-#Schools", 
"TRT(Prog>=50%)- #Schools", "TRT(Prog >= 50% & Enroll>=85%)-#Schools",
"Control-#Schools","TRT-#Grades","TRT(Enroll>=85%)-#Grades", 
"TRT(Prog>=50%)-#Grades", "TRT(Prog >= 50% & Enroll>=85%)-#Grades",
"CTRL-#Grades","TRT-#Students","TRT(Enroll>=85%)-#Students", 
"TRT(Prog>=50%)-#Students", "TRT(Prog >= 50% & Enroll>=85%)-#Students",
"CTRL-#Students")

for (i in 1:ncol(desc.matrix)){
   desc.matrix[,i] <- c(1:(nrow(desc.matrix)))
}

xtable(desc.matrix)

I appreciate your help.

Sam
  • 4,357
  • 6
  • 36
  • 60
  • It would certainly be possible, but you have not provided the data on which to work. I wonder how many other of those unanswered question you fail to cite also fail the test of having a reproducible example? – IRTFM Aug 09 '12 at 18:58
  • Hi DWin, I was thinking of getting some clues and write my code myself. Also, in order to generate these numbers, we should use the corresponding dataset. I'm going to simulate some numbers and I'll add them to the question. – Sam Aug 09 '12 at 19:04

1 Answers1

4

Search for xtableGallery.pdf and search within it for hline.after to find an example of how to put your separator lines anywhere you like. If you do ?xtable then go the index at the bottom and then follow the link at the top about vignettes you can find it there too.

Bryan Hanson
  • 6,055
  • 4
  • 41
  • 78
  • I appreciate Bryan. How about the merging part. I'd like to write the word "Schools" in front of the first 5 rows. Then "Grades" for the next 5 rows and finally "Students" for the last 5 rows. – Sam Aug 09 '12 at 21:57
  • Turned 90 degrees? That'll be tough/impossible. You could add these categories as new rows over their corresponding section, and then set them off with more hlines (which means adding hlines to the example you already have). Assuming you want to keep this totally automated by your Sweave or knitr system I don't think you could do much more. It's hard to combine R data with fancier LaTeX stuff automatically. Hell, tables can be tough in LaTeX generally. There's an SO on LaTeX at http://tex.stackexchange.com/ which might be helpful. – Bryan Hanson Aug 09 '12 at 22:06
  • @Sepehr If this turns out to be your best answer please accept it (green check mark) so I can take over the world with my huge reputation! – Bryan Hanson Aug 10 '12 at 15:32
  • Hi @Bryan, it's actually not. Since the main issue is to merge the labels "schools",... . If you don't mind I wait for couple of days and then I'll mark it as accepted since I think once it's marked as accepted, then people think the question has been answered. I gave you +1 though. – Sam Aug 10 '12 at 15:36
  • Here's a link to several ways to do it, but you'll have to manually insert a few lines into your output so it won't work w/o intervention. You an also color cells this way, rather nice: http://tex.stackexchange.com/questions/9893/how-to-rotate-text-in-table-across-column-spanning-many-rows – Bryan Hanson Aug 11 '12 at 13:35
  • I appreciate it Bryan. But the post above is in LaTex. Do you know of anyway to manipulate the code generated from xtable? The xtable above is part of a chunk code in a big R data analysis project. I don't want to manually print xtable and modify it manually. What I'd like to do is to somehow find a way to modify the xtable in the code without manually changing the latex code. Thanks for your helo Bryan. – Sam Aug 12 '12 at 00:40
  • I guess you could always get the source code out of xtable and modify it to suit your needs. Data frames are handled a certain way. You could copy that code into a R chunk and modify it in your document so that it nominally only applies to the document you Sweave/Knit. This would only work if the modification you need is consistent from chunk to chunk. – Bryan Hanson Sep 11 '12 at 15:04