[R] Formatting zeroes with prettyNum

Marttila Mikko m|kko@m@rtt||@ @end|ng |rom or|onph@rm@@com
Fri May 23 14:29:24 CEST 2025


(In hindsight I should have used a `zero.print` value that looked less like a typo, e.g. `zero.print = "--"` in my example. Sorry about the potential confusion.)

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Marttila Mikko via R-help
Sent: Friday, 23 May 2025 13:21
To: Howard, Tim G (DEC) <tim.howard using dec.ny.gov>; R mailing list <r-help using r-project.org>
Subject: Re: [R] Formatting zeroes with prettyNum

Tim,

The purpose of the `replace.zero` argument is to allow longer replacements.

See for example the behaviour with a character vector input:

> prettyNum("0", zero.print = "- ", replace.zero = TRUE)
[1] "- "

I'm pretty sure this is a bug, and have now posted the relevant details on r-devel.

A fix would be below.

Best,

Mikko

diff --git a/src/library/base/R/format.R b/src/library/base/R/format.R index 63fef83..1afadd3 100644
--- a/src/library/base/R/format.R
+++ b/src/library/base/R/format.R
@@ -338,7 +338,7 @@ prettyNum <-
                    big.mark=big.mark, big.interval=big.interval,
                    small.mark=small.mark, small.interval=small.interval,
                    decimal.mark=decimal.mark, zero.print=zero.print,
-                   drop0trailing=drop0trailing, ...)
+                   replace.zero=replace.zero,
+ drop0trailing=drop0trailing, ...)
     }
     ## be fast in trivial case, when all options have their default, or "match"
     nMark <- big.mark == "" && small.mark == "" && (notChar || decimal.mark == input.d.mark)

-----Original Message-----
From: Howard, Tim G (DEC) <tim.howard using dec.ny.gov>
Sent: Friday, 23 May 2025 13:18
To: Marttila Mikko <mikko.marttila using orionpharma.com>; R mailing list <r-help using r-project.org>
Subject: RE: Formatting zeroes with prettyNum

[You don't often get email from tim.howard using dec.ny.gov. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

You fed it two characters, "dash" and "space" and it only wanted one character so it truncated to the first character and warned you that that's what it did.

If you had your space before the dash, it would have used that as your replacement character

> prettyNum(0, zero.print = " - ", replace = TRUE)

[1] " "

Warning message:
In .format.zeros(x, zero.print, replace = replace.zero) :
  'zero.print' is truncated to fit into formatted zeros; consider 'replace=TRUE'


When zero.print is only one character, it doesn't give a warning.


Tim

> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Marttila
> Mikko via R-help
> Sent: Friday, May 23, 2025 7:07 AM
> To: R mailing list <r-help using r-project.org>
> Subject: [R] Formatting zeroes with prettyNum
>
> ATTENTION: This email came from an external source. Do not open
> attachments or click on links from unknown senders or unexpected emails.
>
>
> Dear list,
>
> Have I stumbled upon a bug, or am I holding `prettyNum()` wrong?
> Please see below.
>
> Best,
>
> Mikko
>
> > prettyNum(0, zero.print = "- ", replace.zero = TRUE)
> [1] "-"
> Warning message:
> In .format.zeros(x, zero.print, replace = replace.zero) :
>   'zero.print' is truncated to fit into formatted zeros; consider 'replace=TRUE'
>
> > prettyNum(0, zero.print = "- ", replace = TRUE)
> [1] "-"
> Warning message:
> In .format.zeros(x, zero.print, replace = replace.zero) :
>   'zero.print' is truncated to fit into formatted zeros; consider 'replace=TRUE'
>
>
>
> This e-mail transmission may contain confidential or legally
> privileged information that is intended only for the individual or
> entity named in the e- mail address. If you are not the intended
> recipient, you are hereby notified that any disclosure, copying,
> distribution, or reliance upon the contents of this e-mail is strictly
> prohibited. If you have received this e-mail transmission in error,
> please reply to the sender, so that they can arrange for proper delivery, and then please delete the message from your computer systems. Thank you.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.e/
> thz.ch%2Fmailman%2Flistinfo%2Fr-
> help&data=05%7C02%7Ctim.howard%40dec.ny.gov%7Cca52f6706ec24a30
> d68108dd99e9f6eb%7Cf46cb8ea79004d108ceb80e8c1c81ee7%7C0%7C0%
> 7C638835952464135918%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hc
> GkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIld
> UIjoyfQ%3D%3D%7C60000%7C%7C%7C&sdata=hxfpM4LqaLhNXyPEbPXtck
> 7QNxBfFvak8HMpcxfAhgE%3D&reserved=0
> PLEASE do read the posting guide
> https://www/.
> r-project.org%2Fposting-
> guide.html&data=05%7C02%7Ctim.howard%40dec.ny.gov%7Cca52f6706ec
> 24a30d68108dd99e9f6eb%7Cf46cb8ea79004d108ceb80e8c1c81ee7%7C0
> %7C0%7C638835952464154131%7CUnknown%7CTWFpbGZsb3d8eyJFbXB
> 0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFp
> bCIsIldUIjoyfQ%3D%3D%7C60000%7C%7C%7C&sdata=V7UNP8FDNZaNGal
> uKgb4Kjr6qSCVLhAhk5vU2y1sixg%3D&reserved=0
> and provide commented, minimal, self-contained, reproducible code.


This e-mail transmission may contain confidential or legally privileged information that is intended only for the individual or entity named in the e-mail address. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or reliance upon the contents of this e-mail is strictly prohibited. If you have received this e-mail transmission in error, please reply to the sender, so that they can arrange for proper delivery, and then please delete the message from your computer systems. Thank you.

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


This e-mail transmission may contain confidential or legally privileged information that is intended only for the individual or entity named in the e-mail address. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or reliance upon the contents of this e-mail is strictly prohibited. If you have received this e-mail transmission in error, please reply to the sender, so that they can arrange for proper delivery, and then please delete the message from your computer systems. Thank you.



More information about the R-help mailing list