Exporting R tables in LaTeX

October 12, 2016
By

(This article was first published on R – Modern Forecasting, and kindly contributed to R-bloggers)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

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)