Here is sample data:
# create table data
year <- c("2010", "2011", "2012")
value <- ("5", "10", "15")
df<-data.frame(year, value)
# print
print(xtable(df,digits=c(0,0,1)))
How can I format the 5, 10, and 15 to display as $5.0, $10.0, and $15.0?
Possible R packages to assist are xtable
, stargazer
, and/or Hmisc
.
Within xtable
, the solution may be found in format.args
: "List of arguments for the formatC
function. For example, standard German number separators can be specified as format.args=list(big.mark = "’", decimal.mark = ","))
." - http://cran.r-project.org/web/packages/xtable/xtable.pdf
Thank you
It is simpler to format the df$value
with dollar format before using the xtable()
function. Using your example you have the following (which requires the scales
library):
library(xtable)
library(scales)
# create table data
year <- c("2010", "2011", "2012")
value <- c("5", "10", "15")
df<-data.frame(year, value)
df$value <- dollar(as.numeric(as.character(df$value)))
# print
print(xtable(df))
This produces the following LaTeX Code:
% latex table generated in R 2.12.2 by xtable 1.6-0 package
% Fri May 03 10:46:58 2013
\begin{table}[ht]
\begin{center}
\begin{tabular}{rll}
\hline
& year & value \\
\hline
1 & 2010 & \$5.00 \\
2 & 2011 & \$10.00 \\
3 & 2012 & \$15.00 \\
\hline
\end{tabular}
\end{center}
\end{table}