Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display trailing zeros with signif

I am creating tables displaying numerical data with the gt package in R. I need to display 2 significant figures in my table and want to show trailing zeros if relevant (for aesthetic purposes). For example, I need the number 0.301 to display as 0.30. The number 0.000502 to display as 0.00050. How do I do this? The function signif() displays these values as 0.3 and 0.0005.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Just format the values using sprintf before printing them:

sprintf('%#.2g', c(0.301, 0.000502))
[1] "0.30"    "0.00050"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading