Formatting numbers for display
HI,
I modified the code, thanks to Tejas.? Now, the results look like
fun1<-function(x)
?ifelse(x>10^7,gsub("\\d)[.].*","\\1",x),ifelse(x<10^7 & x>1,formatC(x,width=length(x),format="fg",digits=8,flag="",drop0trailing=FALSE),sprintf("%.3f",x)))
fun1(VALUES)
?[1] "123456789"??? "12345678"???? "?? 1234567.9" "?? 123456.89" "?? 12345.789"
?[6] "?? 1234.6789" "?? 123.56789" "?? 12.456789" "?? 1.3456789" "0.123"??????
[11] "0.012"??????? "0.001"???
David's code:
? form3 <- function (x) switch(findInterval(x, c( 0, 1, 10^7, Inf)),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? format(x, digits=3),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? formatC(x, width=8, ?format="f", ?
drop0trailing=FALSE),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? trunc(x) )
?sapply(VALUES,form3)
?[1] "123456789"??? "12345678"???? "1234567.9000" "123456.8900"? "12345.7890"?
?[6] "1234.6789"??? "123.5679"???? " 12.4568"???? "? 1.3457"???? "0.123"??????
[11] "0.0123"?????? "0.00123"
#Modfied form3 a little:
form3 <- function (x) switch(findInterval(x, c( 0, 1, 10^7, Inf)),
????????????????????????????????????? formatC(x, width=8, format="f",digits=3),
????????????????????????????????????? formatC(x, width=8,? format="f",?
drop0trailing=TRUE,flag="0"),
????????????????????????????????????? trunc(x) )
sapply(VALUES,form3)
?[1] "123456789" "12345678"? "1234567.9" "123456.89" "12345.789" "1234.6789"
?[7] "123.5679"? "012.4568"? "001.3457"? "?? 0.123"? "?? 0.012"? "?? 0.001"
Still, there needs improvement.
? ?
A.K.
?
----- Original Message -----
From: David Winsemius <dwinsemius at comcast.net>
To: Dennis Fisher <fisher at plessthan.com>
Cc: arun <smartpink111 at yahoo.com>; R help <r-help at r-project.org>
Sent: Friday, August 3, 2012 3:14 AM
Subject: Re: [R] Formatting numbers for display
On Aug 2, 2012, at 8:27 PM, arun wrote:
For your first condition, this could be used:
gsub("(\\d)[.].*","\\1",498888888.85)
[1] "498888888"
Second condition:
formatC(4333.78889,width=8,format="f",digits=2,flag="0")
#[1] "04333.79"
? formatC(884333.78889,width=8,format="f",digits=2,flag="0")
#[1] "884333.79"
Third condition:
sprintf("%.3f",0.0123556)
#[1] "0.012"
I didn't get the same results as requested with a wider variety of tests using those formatting methods. Here's what I could offer: form3 <- function (x) switch(findInterval(x, c( 0, 1, 10^7, Inf)), ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? format(x, digits=3), ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? formatC(x, width=8,? format="f", drop0trailing=FALSE), ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? trunc(x) ) tx <- c( 0.55555555, 55.555555555, 555555555555555.55555555)
sapply(tx, form3)
[1] "0.556"? ? ? ? ? " 55.5556"? ? ? "555555555555555" (Still not getting the 8 digits in the intermediate value cases. Have not figured out how to get rid of leading space.) --David.
#You can create a function similar to this. (This still have some bugs).
? fun1<-function(x)
? ifelse(x>10^7,gsub("\\d)[.].*","\\1",x),ifelse(x<10^7 & x>1,formatC(x,width=8,format="f",digits=2,flag="0"),sprintf("%.3f",x))
? )
fun1(488.85)
#[1] "00488.85"
? fun1(0.1233555)
#[1] "0.123"
Using the vector of long 5's
sapply(tx, fun1)
[1] "0.556"? ? ? ? ? "00055.56"? ? ? ? "555555555555556"
fun1(VALUES) # [1] "123456789"? "12345678"? "1234567.90" "123456.89"? "12345.79" ? #[6] "01234.68"? "00123.57"? "00012.46"? "00001.35"? "0.123" #[11] "0.012"? ? ? "0.001" A.K. ----- Original Message ----- From: Dennis Fisher <fisher at plessthan.com> To: r-help at stat.math.ethz.ch Cc: Sent: Thursday, August 2, 2012 10:34 PM Subject: [R] Formatting numbers for display 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
David Winsemius, MD Heritage Laboratories West Hartford, CT