【问题标题】:Adding a percent symbol to xtabs cells in r向 r 中的 xtabs 单元格添加百分比符号
【发布时间】:2013-06-27 23:32:09
【问题描述】:

我正在尝试使用 R 包 surveypander 来快速生成表格和报告,但似乎无法按照我需要的方式格式化表格。我想在表格的相应单元格之后添加百分比符号。

这是我生成表格的代码。单元格被标准化为 100,所以我只需要以某种方式将百分号 (%) 添加到降价输出中。

library(pander)
library(survey)
employee_id <- c(1020:1069)
Q9_1 <- rep(as.factor(c('Unsatisfied','Neutral','Satisfied','Satisfied','Satisfied')),10)
employee <- data.frame(employee_id,Q9_1)
employee_survey <- svydesign(id=~1, weights=~1, data=employee)
pandoc.table(svytable(~Q9_1,employee_survey,Ntotal=100))

我只是在这里错过了一个简单的选择吗?我搜索了又搜索,但没有找到任何有用的东西。

【问题讨论】:

  • 所以我们应该猜测'employee'的结构还是提供替代品?为什么不在可剪切的形式中包含包的名称和示例数据?
  • 为什么不将% 放在行或列标签中。这意味着表中的“噪音”更少。 AFAIK 在pandoc.table 中没有以这种方式处理值的选项

标签: r pander


【解决方案1】:

可能是这样的,显然用employee_survey 替换tbl ...仍然未经测试。

scratch <- attr(tbl, "dimnames")
scratch$stype=paste(scratch$stype, "%")
scratch -> attr(tbl, "dimnames")
pandoc.table(tbl)

在单元格内容上可能有一些类似的过程。再次仅在?svytable 中的示例上进行了测试:

 ntbl <- as.character(round(tbl, 2))
 ntbl <- paste(ntbl, "%")
 attributes(ntbl) <- attributes(tbl)
 ntbl
 #---------------    
    stype
sch.wide E        H        M       
     No  406.16 % 101.54 % 270.78 %
     Yes 4467.8 % 372.32 % 575.4 % 
 #-------------
pandoc.table(ntbl)
#----------------------------------

------------------------------------
 &nbsp;      E        H        M    
--------- -------- -------- --------
 **No**   406.16 % 101.54 % 270.78 %

 **Yes**  4467.8 % 372.32 % 575.4 % 
------------------------------------

根据您希望有效数字的显示方式,这也是可能的:

 ntbl <- format(round(tbl, 2),digits=5)
 attributes(ntbl) <- attributes(tbl)
 pandoc.table(ntbl)

-------------------------------
 &nbsp;      E      H      M   
--------- ------- ------ ------
 **No**   406.16  101.54 270.78

 **Yes**  4467.80 372.32 575.40
-------------------------------

【讨论】:

  • 这很好用。感谢您的快速工作,DWin。百分号 (%) 和数字之间的间距可以由 paste 函数中的 sep 参数控制,即 sep=""。
猜你喜欢
  • 1970-01-01
  • 2014-03-10
  • 1970-01-01
  • 2021-09-27
  • 2013-06-23
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
相关资源
最近更新 更多