I'm trying to use the R packages survey
and pander
for some quick table and report generation, but can't seem to format a table the way I need. I'd like to add percent symbols after the appropriate cells to the table.
Here's my code to generate the table. The cells are normalized to 100, so I just need to add the percent symbol (%) somehow to the markdown output.
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))
Am I just missing a simple option here? I've searched and searched, but didn't find anything useful.
Probably something along these lines, obviously replacing tbl
with employee_survey
...still untested.
scratch <- attr(tbl, "dimnames")
scratch$stype=paste(scratch$stype, "%")
scratch -> attr(tbl, "dimnames")
pandoc.table(tbl)
A somewhat similar process is possible on the cell contents. Again tested only on the example in ?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 %
------------------------------------
Depending on how you want the significant digits displayed this is also possible:
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
-------------------------------