Printing tables in R
Use the xtable package, which has a print function for HTML or LaTeX.
[edit] Formatting numbers beforehand
xtable will format to a given number of significant digits, but sometimes you need other number formatting. Put your data into a data.frame. Example:
Total Cutoffs Included % cutoff O85% 355321 0 273415 23.051269 D85% 130752 0 70613 45.994708 O60% 946010 4 4142855 5.350694 D60% 4377058 4 842646 10.926312 O40% 14249207 10 13824348 2.981633 D40% 3208833 10 2969997 7.443080
The Total and Included would be better if there were commas every three places.
# a function that will take in a big integer and # return a string of it with commas big_int_format <- function(x) { formatC(x, big.mark=",", format="d") } # replace those columns with formatted ones num_aligns$Total <- sapply(num_aligns$Total, FUN=big_int_format) num_aligns$Included <- sapply(num_aligns$Included, FUN=big_int_format)
Now, those columns will be characters that include commas. Note: Any further number formatting needed in those columns, such as significant digits, should be done with the formatC function in big_int_format, because xtable won't do it afterwards. The new table:
Total Cutoffs Included % cutoff O85% 355,321 0 273,415 23.051269 D85% 130,752 0 70,613 45.994708 O60% 946,010 4 4,142,855 5.350694 D60% 4,377,058 4 842,646 10.926312 O40% 14,249,207 10 13,824,348 2.981633 D40% 3,208,833 10 2,969,997 7.443080
Note that xtable won't automatically align those two columns to the right anymore, so the alignment should be manually specified for each column:
print(xtable(num_aligns, digits=c(0,0,0,0,3), align=c("l","r","r","r","r")), type="html")