Exporting R tables in LaTeX

Recently I have started using LaTeX for all my documents and presentations. Don’t ask me why, I just like how texts look there rather than in products of Microsoft (and I in general dislike MS… we have a long unpleasant history). So, I sometimes need to export tables from R into LaTeX. These tables can be huge, so exporting them manually is not an option. I know that there are R packages for this (probably, R has packages for everything in this world), however I prefer simpler methods. So what I usually do is just use write.table() function:

ourTable <- matrix(c(1:3), 3, 3)
dimnames(ourTable) <- list(paste0("Row",c(1:3)), paste0("Col",c(1:3)))

write.table(ourTable, "ourTable.txt", quote=FALSE, eol="\\\\\n", sep=" & ")

As a result that matrix is saved in “ourTable.txt” file:

Col1 & Col2 & Col3\\
Row1 & 1 & 1 & 1\\
Row2 & 2 & 2 & 2\\
Row3 & 3 & 3 & 3\\

and it can be easily copy-pasted into desired LaTeX file. The only thing that needs to be done after that – is correction of column names with additional “&” before “Col1”.

Leave a Reply