R Data.Frame To Latex Table With Formatting Of Decimal Points
3
3
Entering edit mode
11.4 years ago
Paul ▴ 760

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 • 5.7k views
ADD COMMENT
1
Entering edit mode

Have you tried "?sprintf"?

ADD REPLY
0
Entering edit mode

Have you tried ?sprintf?

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
3
Entering edit mode
11.4 years ago
Thomas ▴ 750

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

ADD COMMENT
0
Entering edit mode

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.

ADD REPLY
3
Entering edit mode
11.4 years ago

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)
ADD COMMENT
2
Entering edit mode

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="")) } }

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
3
Entering edit mode
11.4 years ago
Sander Timmer ▴ 710

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

ADD COMMENT

Login before adding your answer.

Traffic: 2662 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6