This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R Data.Frame To Latex Table With Formatting Of Decimal Points

Hi, I'm wondering if there is an easy way to output an R data.frame to nicely formatted LaTex table. I have a column of snp names (character vector) and a column of p-values (numeric vector) which look like "0.0001814571" that I'd like to format like "1.81 \times 10^{-4}" etc. I've found no easy way of doing this though, I wonder will I have to do it manually?

Thanks!

Paul.

r

Sweave is an R tool for generating documents, with tables support. Check that out. I dont have the experience with it to make this a proper Answer.

3 answers

Sorry if I misunderstand your Qs... however when I convert R-dataframes to latex documents. I always use the Hmisc package and the latex function. Seems to work perfect:

library(Hmisc) ###an example of a dataframe dat<-data.frame(cbind(paste("rs",1:5,sep=""),c(0.1,0.00001,0.2,0.000004,0.4))) ###the latex function latex(dat)

all the best Thomas

Ya that's basically it, I'm wondering if there's any way to format the numbers nicely though, i.e. "1.81 times 10^{-4}". It's probably fairly easily doable with find and replace in a text editor though.

Expanding on the sprintf comment, you could try something like this:

nums = c(0.1,0.00001,0.2,0.000004,0.4)
r2l = function(n) sub("E", " \times 10^{", sprintf("%0.3E}", n))
latex = lapply(nums, r2l)

Thanks for that, there's a slightly improved versino of the function that doesn't do the 10^ bit for bigger numbers:

r2l = function(n) { if(n < .01) { return(paste("$", sub("E", " \\times 10^{", sprintf("%0.2E}", n)), "$", sep="")) } else if(n >=.01) { return(paste("$", format(n, digits=2), "$", sep="")) } }

You could also try the above code with "%0.3g" ;)

You could also try "%0.3G" but you would still need to fix the curly brackets.

You can also use the xtable package in R to write to Latex or HTML. Though this is not for data.frames but for matrixes.

The good thing about xtable is that you can specify how xtable should deal with small numbers in your matrix. You can use the display option to give a vector for each column:

display Character vector of length equal to the number of columns of the resulting table indicating the format for the corresponding columns. Since the row names are printed in the first column, the length of display is one greater than ncol(x) if x is a data.frame. These values are passed to the formatC function. Use "d" (for integers), "f", "e", "E", "g", "G", "fg" (for reals), or "s" (for strings). "f" gives numbers in the usual xxx.xxx format; "e" and "E" give n.ddde+nn or n.dddE+nn (scientific format); "g" and "G" put x[i] into scientific format only if it saves space to do so. "fg" uses fixed format as "f", but digits as number of significant digits. Note that this can lead to quite long result strings. Default depends on the class of x.

See for the full usage of this package the following link: http://rss.acs.unt.edu/Rdoc/library/xtable/html/00Index.html

Log in to answer this question.