Creating LaTeX tables

Average: 4 (1 vote)

If you aren't using LaTeX for reports, then you should be. Other than beautiful equation type-setting and layout flexibility, you can automatically turn data tables in R (or matrices, to be exact) into latex tables using the latex.table function in the quantreg library.

Let's say we want to create a table of means and standard deviations for the sepal length and width variables in Fisher's class iris data set.

library(quantreg)
data(iris)
my.table <- rbind(
     c(mean(iris$Sepal.Length), mean(iris$Sepal.Width)),
     c(sd(iris$Sepal.Length), sd(iris$Sepal.Width)))
colnames(my.table) <- c('Sepal Length', 'Sepal Width')
rownames(my.table) <- c('Mean', 'SD')
my.table

my.table now looks like:

     Sepal Length Sepal Width
Mean    5.8433333   3.0573333
SD      0.8280661   0.4358663

And now the magic:

latex.table(my.table, file="iris", rowlabel="", caption="Mean and SD of Iris data")

This will write a file called iris.tex in your current working directory. You can "include(iris)" in your latex source, or just copy and paste the LaTeX code in iris.tex into your source document.

AttachmentSize
iris_latex_table.pdf9.99 KB