3

Given the following example:

library(pander)
tableAbs <- Titanic[1, 1, , ]
tablePct <- round(prop.table(tableAbs) * 100, 2)
table <- cbind(tableAbs, tablePct)
pander(table)

----------------------------------
  &nbsp;     No   Yes   No    Yes 
----------- ---- ----- ----- -----
 **Child**   0     5     0   2.78 

 **Adult**  118   57   65.56 31.67
----------------------------------

I would like to keep all trailing zeros on that 0 percentage over there, so this is what I do:

panderOptions("keep.trailing.zeros", TRUE)
pander(table)

------------------------------------
  &nbsp;      No    Yes   No    Yes 
----------- ------ ----- ----- -----
 **Child**   0.00  5.00  0.00  2.78 

 **Adult**  118.00 57.00 65.56 31.67
------------------------------------

The problem is now that even the absolute frequencies have .00 attached to them. Since those are natural numbers, it makes little sense to keep those trailing zeros. How do I do this?

Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107
  • I'm not an experienced `r` user, but do you think you could call `gsub` or something on the elements of `table`? If so, this is a piece of cake using a simple [tag:regex]. – ShellFish May 29 '15 at 21:01
  • 2
    Try `tablePct <- format(round(prop.table(tableAbs) * 100, 2))` – user20650 May 29 '15 at 21:08
  • @user20650, that works, thanks! But why? – Waldir Leoncio May 29 '15 at 21:11
  • 1
    I was going to suggest converting to character (or using `sprintf('%.2f', table)` or whatever) and then reapplying the attributes to get the structure back, but it seems like that is what format does in one step by preserving the dimensions. – rawr May 29 '15 at 21:15
  • @rawr, in any case, would you consider writing your solution as an answer so it's more visible? – Waldir Leoncio May 29 '15 at 21:18
  • @user20650, would you consider writing your solution as an answer so it's more visible? – Waldir Leoncio May 29 '15 at 21:19
  • @rawr: thats what i did first `tablePct[] <- sapply(tablePct, sprintf, fmt="%.2f")` .. do you want to write it up .. you have a better understanding of such stuffs – user20650 May 29 '15 at 21:21
  • your idea is more elegant. I just regurgitated what it says under value in `?format` – rawr May 29 '15 at 21:22

1 Answers1

2

Thanks to rawr in the comments.

You can use format to keep the trailing zeros. This converts the rounded values to character while preserving the dimensions of the table.

tablePct <- format(round(prop.table(tableAbs) * 100, 2))

edit

Seems to work okay with xtabs class

mtcars$am[mtcars$vs == 1]  <- 0
x <- xtabs(~ am + vs, data=mtcars)
tab <- format(round(100*prop.table(x), 2))
tab <- cbind(x, tab)
pander(tab)

---------------------------
&nbsp;   0   1    0     1  
------- --- --- ----- -----
 **0**  12  14  37.50 43.75

 **1**   6   0  18.75 0.00 
---------------------------
user20650
  • 24,654
  • 5
  • 56
  • 91
  • This does it for the mock data. Unfortunately, it doesn't seem to work with my real table, which is of the `xtabs` class (and `format` transforms numbers into characters). I'll see if I can work around that. – Waldir Leoncio May 29 '15 at 21:33
  • Waldir, please feel free to edit your question with an xtabs example – user20650 May 29 '15 at 21:35
  • 1
    Thanks, but I got it. Took a little butchering and stiching, but it's finally looking beautiful. – Waldir Leoncio May 29 '15 at 21:46