gpt4 book ai didi

r - 向 r 中的 xtabs 单元格添加百分号

转载 作者:行者123 更新时间:2023-12-04 12:52:51 24 4
gpt4 key购买 nike

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

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

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

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

关于r - 向 r 中的 xtabs 单元格添加百分号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17354930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com