千位和十进制数的分隔(库“ pivottabler”)

时间:2019-12-05 19:14:41

标签: r formatting

我需要带点的千位分隔符和带逗号的十进制分隔符。我的数据是图书馆透视表的bhmtrains。

我正在使用以下代码:

 library(pivottabler)

  qhpvt(bhmtrains, "TOC", "TrainCategory",
  c("Mean Speed"="format(sum(SchedSpeedMPH, na.rm=TRUE))", "Std Dev Speed"="sd(SchedSpeedMPH,na.rm=TRUE)"),
  formats=list("%.0f", "%.1f%%"), totals=list("TOC"="All TOCs", "TrainCategory"="All Categories"))

示例:  输入:12000.2

输出:12.000,2

1 个答案:

答案 0 :(得分:1)

这可以通过将每个计算的格式设置指定为列表来完成。列表中的元素由base::format()作为参数传递给R pivottabler函数。

作为一个例子,从这个样本数据开始(因为问题中的样本数据以标准差作为百分比使我感到困惑):

library(pivottabler)

qhpvt(bhmtrains, "TOC", "TrainCategory",
      c("Value 1"="mean(SchedSpeedMPH, na.rm=TRUE)*33.33333", 
        "Value 2"="sd(SchedSpeedMPH,na.rm=TRUE)*333.33333"),
      formats=list("%.5f", "%.5f"), 
      totals=list("TOC"="All TOCs", "TrainCategory"="All Categories"))

enter image description here

将格式设置指定为列表:

library(pivottabler)

qhpvt(bhmtrains, "TOC", "TrainCategory",
      c("Value 1"="mean(SchedSpeedMPH, na.rm=TRUE)*33.33333", 
        "Value 2"="sd(SchedSpeedMPH,na.rm=TRUE)*333.33333"),
      formats=list(list(digits=1, nsmall=0, big.mark=".", decimal.mark=","), 
                   list(digits=1, nsmall=1, big.mark=".", decimal.mark=",")), 
      totals=list("TOC"="All TOCs", "TrainCategory"="All Categories"))

结果:

enter image description here