Adding a horizontal line between rows in a latex table using R xxtable

I have a sample latex table generation code such as

df<-data.frame(name=rep(letters[1:7],each=24),salary=runif(24*7,100,200)) lst<-tapply(df$salary,df$name,matrix,nrow=4,byrow=T) xlst<-lapply(lst,xtable) 

Now I want to enter \ hdashline automatically after each row in each table and also a \ vspace {2em} between the tables from the R-code of Wat, I tried this

 for(i in seq_along(lst)){ addtorow[i] <- list(pos = list(seq_len(nrow(lst[[i]])-1)), command = "\\hdashline \n") } 

Wat changes I need to do in for loop.It works when I apply for one table but do not work in a for loop ... Any help is much appreciated ...

+7
source share
2 answers

Instead of using a loop, consider using the existing print.xtable function in conjunction with an insert.

  • hdashlines: Consider using print.xtable options. For example, there is an hline.after parameter that controls the horizontal lines between the lines of the table.

  • vspace: This is possibly simpler with paste .

For example:

 library(xtable) df <- data.frame(name=rep(letters[1:3],each=24),salary=runif(24*3,100,200)) lst <- tapply(df$salary,df$name,matrix,nrow=4,byrow=T) xlst <- lapply(lst,function(x)print(xtable(x), hline.after=1:nrow(x))) xlst <- lapply(xlst, paste, "\\vspace{2em}") xlst 

[1] "% latex table generated in R 2.13.1 using the xxtable 1.5-6 package \ n% Tue Aug 23 13:36:41 2011 \ n \ BEGIN {table} [XT] \ n \ {start center } \ n \ {start tabular} {RRRRRRR} \ n and 1 and 2 and 3 and 4 and 5 and 6 \\\ n 1 and 158.66 and 115.81 and 106.70 and 128.78 and 157.43 and 191.01 \\\ n \ hline \ n2 and 159.09 and 172.31 and 153.93 & 127.91 and 106.93 and 147.95 \\\ n \ hline \ n3 and 135.65 and 139 , 45 & 192.90 and 108.78 and 186.52 and 164.10 \\\ n \ hline \ n4 and 190.10 & 154.39 and 124.91 and 199.24 and 161.99 and 167.61 \\\ n
\ hline \ n \ end {tabular} \ n \ end {center} \ n \ end {table} \ n \ vspace {2em} "


See ?print.xtable more details. If hline.after not flexible enough for your purposes, see add.to.row , where you can specify both the position and the exact type of element to add.

+10
source

If you are using print.xtable , try the following inside print.xtable :

hline.after = rep(c(1:nrow(df)),2)# repeating horizontal lines in xtable

0
source

All Articles