向r中的xtabs单元格添加百分比符号
问题描述:
我试图使用R程序包survey
和pander
来生成一些快速表格和报表,但似乎无法按照需要格式化表格。我想在适当的单元格后面添加百分号。向r中的xtabs单元格添加百分比符号
这里是我的代码生成表。单元格标准化为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))
我在这里只是错过了一个简单的选项?我搜索过,但没有找到有用的东西。
答
可能是沿着这些路线的东西,显然将取代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)
#----------------------------------
------------------------------------
E H M
--------- -------- -------- --------
**No** 406.16 % 101.54 % 270.78 %
**Yes** 4467.8 % 372.32 % 575.4 %
------------------------------------
根据您想要的显著数字dispalyed这是阿洛斯可能:
ntbl <- format(round(tbl, 2),digits=5)
attributes(ntbl) <- attributes(tbl)
pandoc.table(ntbl)
-------------------------------
E H M
--------- ------- ------ ------
**No** 406.16 101.54 270.78
**Yes** 4467.80 372.32 575.40
-------------------------------
+0
这很好。感谢您的快速工作,迪文。百分比符号(%)和数字之间的间距可以通过粘贴功能中的sep参数进行控制,即sep =“”。 – Sterling
因此,我们应该在“员工”或结构猜测提供替代品?为什么不将包装和样本数据的名称包含在可剪切的表格中? –
为什么不在行或列标签中放置'%'。这意味着表中的“噪音”较少。 AFAIK在'pandoc.table'内没有选项来处理这个值 – mnel