5

How can I keep pander from dropping the trailing zeros when I'm simultaneously telling it to round values above their respective number of significant figures?

In the following reproducible example, I'm trying to round a column of p-values for two hypothetical variables (male and female).

require(pander)

Gender <- c("Female", "Male")
p <- c(4.342e-06, 1.325e-05)
table <- data.frame(Gender, p)

panderOptions('round', 4)
panderOptions('keep.trailing.zeros', TRUE)

pandoc.table(table,
             round = panderOptions("round"),
             keep.trailing.zeros = panderOptions("keep.trailing.zeros"))

What I get, however, is

------------
 Gender   p 
-------- ---
 Female   0 

  Male    0 
------------

But what I want to get is

----------------
 Gender      p 
-------- -------
 Female   0.0000 

  Male    0.0000
----------------

How can I make that happen?

**PS: I'm aware of this question, but it does not solve my problem.*

Edit: I'm also aware of the question here, but the solution there does exactly what I'm trying to keep from happening here, that is, dropping the trailing zeros when I'm trying to round to value that does not contain any significant figures for all my variables. NicE has a solution (which rawr also gave in the aforementioned previous question), but they require converting the values to characters in addition to stacking up extra code, which I would like to avoid if possible.

Community
  • 1
  • 1
Johan Larsson
  • 3,496
  • 18
  • 34
  • 2
    Not sure how to fix this using `pander` options but you could format the numbers before printing the table, for ex `p <- sprintf("%.4f",round(p,digits=4))` would output what you want – NicE Jul 24 '15 at 10:56
  • possible duplicate of [Keep trailing zeros for percents only](http://stackoverflow.com/questions/30538920/keep-trailing-zeros-for-percents-only) – user20650 Jul 24 '15 at 12:20
  • Thanks! This seems like a pretty good work-around, but I'm still hoping for something more streamlined if possible. If not, perhaps I should rather make a suggestion on github instead. – Johan Larsson Jul 24 '15 at 12:27
  • user20650 — If you mean the format() + round() solution presented therein, it does the equivalent of setting panderOptions('keep.trailing.zeroes', TRUE) which is not helping me here since it also drops the trailing zeros when the values I get after rounding contains no significant figures. – Johan Larsson Jul 24 '15 at 13:12

1 Answers1

0

pander (or more precisely pandoc.table) first rounds round the number to the given number of digits, so see e.g. the following base example:

> round(c(4.342e-06, 1.325e-05), 4)
[1] 0 0

Now you cannot keep the trailing zeros as there are none. On the other hand, if one of the numbers are a bit higher so that we do not round to zero, like e.g.:

> round(c(4.342e-06, 1.325e-05), 5)
[1] 0e+00 1e-05

There's some trailing zeros we can keep :) Based on this please see the following two examples:

> pander(matrix((c(4.342e-06, 1.325e-05))), round = 5)

-----
0e+00

1e-05
-----

> pander(matrix((c(4.342e-06, 1.325e-04))), round = 5)

-------
0.00000

0.00013
-------

In short, pander provides no ways to keep the trailing zeros if there's none -- if you need that, you either need a value in the column with more digits (so that pander can fill in trailing zeros for the other numbers) or do some preprocessing like @NicE suggested.

daroczig
  • 28,004
  • 7
  • 90
  • 124
  • Thanks for the instructive answer! I had figured out this feature already, but I don't find it very agreeable since I'm usually constructing larger tables than this and want to maintain a reasonable number of decimals for the other values. I think I'd prefer it if pandoc.table used sprintf(), or perhaps parsed it to be formatted in a LaTeX package such as [siunitx](https://www.ctan.org/pkg/siunitx), alternatively that it was possible to set rounding per column in pandoc.table, but I suppose this is an issue for pandoc rather than pander. – Johan Larsson Jul 24 '15 at 21:34
  • 1
    Thanks for the feedback, @Johan. Unfortunately, adding support for `sprintf` would introduce many questions to be answered (how to specify the preferred formatting column-wise etc), but rounding per column in `pandoc.table` and `pander` is already supported since https://github.com/Rapporter/pander/commit/7d4f7198948073df732dd37f07b6561bfde6fe55 (so in the dev version of the package) – daroczig Jul 24 '15 at 22:41