R: formatting the digits in xtable

孤街醉人 提交于 2019-12-22 09:48:16

问题


I have the data:

transaction <- c(1,2,3);
date <- c("2010-01-31","2010-02-28","2010-03-31");
type <- c("debit", "debit", "credit");
amount <- c(-500, -1000.97, 12500.81);
oldbalance <- c(5000, 4500, 17000.81)
evolution <- data.frame(transaction, date, type, amount, oldbalance, row.names=transaction, stringsAsFactors=FALSE);
evolution$date <- as.Date(evolution$date, "%Y-%m-%d");
evolution <- transform(evolution, newbalance = oldbalance + amount);
evolution

If I want to create a table with the digits in amount just equal to 1 decimal place, does such a command work?

> tab.for <- formatC(evolution$amount,digits=1,format="f")
> tab.lat <- xtable(tab.for)
Error in UseMethod("xtable") :
  no applicable method for 'xtable' applied to an object of class "character"

Thanks.


回答1:


If you just want to reformat the amount column and present in an xtable, then you need to supply xtable with a dataframe, just supplying one column makes it looks like a character vector.

evolution$tab.for <- formatC(evolution$amount,digits=1,format="f")

evolutionxtable<-xtable(subset(evolution, select=c(date, type, tab.for)))

print(evolutionxtable)


来源:https://stackoverflow.com/questions/8652731/r-formatting-the-digits-in-xtable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!