在r中的xtabs单元格中添加百分比符号

时间:2013-06-27 23:32:09

标签: r pander

我正在尝试使用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))

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

1 个答案:

答案 0 :(得分:4)

沿着这些方面可能有些东西,显然用tbl取代employee_survey ......仍未经过测试。

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