Exporting R tables in LaTeX

[This article was first published on R – Modern Forecasting, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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”.

To leave a comment for the author, please follow the link and comment on their blog: R – Modern Forecasting.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)