r - How to format columns in xtable output -


i have data.frame df , i'm using xtable create html table:

     product    n° clients     capital (usd) part. capital (%)      1               536            2616925       33.62     2 b               151            1613035       20.72     3 c               112            1007983       12.95 

the problem when generating html:

    df.tab <- xtable(df, align = "cccrc",latex.environments="center", format.args = list(digits = 2, format = c("s","d","d","d","f"), big.mark = ","), floating = false)     print(df.tab,type = "html", include.rownames=false, file = "df.tab.html") 

i expect columns (part. capital) have 2 decimal places (even if elements may integers) , comma separator "," , other columns treated integers result not way:

     product       n° clients    capital (usd) part. capital (%)      1               536          2616925.00       33.62     2 b               151          1613035.00       20.72     3 c               112          1007983.00       12.95 

btw, had align , format "invisible" column because without couldn't work. appreciated.

this should work:

df[,3]<-format(as.numeric(df[,3]),big.mark=",") df.tab <- xtable(df, align = "cccrc",latex.environments="center", format.args = list(digits = 2, format = c("d","d","s","f"))) print(df.tab,type = "html", include.rownames=false, file = "df.tab.html") 

in r results in

     product n° clients capital (usd) part. capital (%) [1,] "a"     "536"      "2,616,925"   "33.62"           [2,] "b"     "151"      "1,613,035"   "20.72"           [3,] "c"     "112"      "1,007,983"   "12.95" 

the html version looks this:enter image description here


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -