【问题标题】:R: formatting the digits in xtableR:格式化xtable中的数字
【发布时间】:2011-12-28 06:41:27
【问题描述】:

我有数据:

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

如果我想创建一个表,其中amount 中的数字正好等于小数点后一位,这样的命令是否有效?

> 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"

谢谢。

【问题讨论】:

    标签: r digits


    【解决方案1】:

    如果您只想重新格式化金额列并显示在 xtable 中,那么您需要为 xtable 提供一个数据框,只需提供一列就会使其看起来像一个字符向量。

    evolution$tab.for <- formatC(evolution$amount,digits=1,format="f")
    
    evolutionxtable<-xtable(subset(evolution, select=c(date, type, tab.for)))
    
    print(evolutionxtable)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      相关资源
      最近更新 更多