Site icon texblog

R: Convert table R2LaTeX

R and LaTeX work very well together. Let me show you how to generate a LaTeX table from a R variable.

First, install the xtable package if it’s not already.

install.packages("xtable")

Next load the package and create the tex-file with the table variable result. For illustration purposes, I create a matrix with random values.

library(xtable)
result <- replicate(6, rnorm(10)) # random matrix
print(xtable(result), type="latex", file="output.tex")

Since you probably don’t need an object that holds the LaTeX code, creating and writing the code to a file is combined in one command.

Note: You can control the number of digits using:

xtable(result, digits=-3)

The minus puts numbers into scientific format.

Take Our Poll
Exit mobile version