[R] Formatting numbers for display
Dennis Fisher
fisher at plessthan.com
Fri Aug 3 04:34:33 CEST 2012
Colleagues
R 2.15.1
OS X
I have a lengthy script that generates a positive number that I display in a graphic using text. The range of possible values is quite large and I am looking for an efficient means to format.
1. If the number is large (e.g., > 10^7), I want to display only the integer portion.
2. If it is less than 10^7 but > 1, I want to display 8 characters, e.g.,
12345.78
1234.678
123.5678
3. If it is less than 1, I want to display at least three significant digits, e.g.
0.123
0.0123
0.00123
0.000123
If there are any inconsistencies in my proposal, I apologize.
I can accomplish this by brute force with conditionals, -ceiling(log10(VALUE)), round. However, I expect that there is a more efficient approach, possibly using sprint.
For the "dput"-ers, use the following as potential input:
VALUES <- c(123456789, 12345678, 1234567.9, 123456.89, 12345.789, 1234.6789, 123.56789, 12.456789, 1.3456789, 0.123456789, 0.0123456789, 0.00123456789)
Thanks for any thoughts.
Dennis
Dennis Fisher MD
P < (The "P Less Than" Company)
Phone: 1-866-PLessThan (1-866-753-7784)
Fax: 1-866-PLessThan (1-866-753-7784)
www.PLessThan.com
More information about the R-help
mailing list